#Raycasts, again

1 messages · Page 1 of 1 (latest)

solar tinsel
#

trying to fire the raycasts every heartbeat when the tool is activated, I'm getting a bit frustrated abt how it's not working

civic bobcat
#

is anything printing from print(ray) when you test?

civic bobcat
#

what else is shown in the output?

#

if the raycast is returning nil it could be that its not hitting anything. The direction Vector3.new(0.5,0.5,0.5) is very small which means it will only go less than 1 stud before ending the raycast i think

solar tinsel
#

like

civic bobcat
#

do you want it to only start the raycasting once the attribute activated is set to true?

civic bobcat
#

ok you can try this instead

#
local hitbox = script.Parent
local tool = hitbox.Parent
local runservice = game:GetService("RunService")
local attachments = hitbox:GetChildren()
local hitevent = game.ReplicatedStorage.Hitbox_Activation
local rayhitbox = nil -- initialize variable for runservice connection here

hitevent.OnServerEvent:Connect(function(plr,state)
    print(state)
    if state == "playing" then
        hitbox:SetAttribute("Activated", true)
    elseif state == "stopped" then
        hitbox:SetAttribute("Activated", false)
    end
end)

local function startRaycast() -- create function to start raycasting connection
    if rayhitbox~=nil then return end
    rayhitbox = runservice.Hearbeat:Connect(function()
        for i,v in attachments do -- dont need pairs anymore in roblox i dont think but u can if u want
            if v:IsA("Attachment") then
                local ray = workspace:Raycast(v.WorldCFrame.Position, Vector3.new(0.5,0.5,0.5))
                print(ray)
            end
        end
    end)
end

local function stopRaycast() -- create function to stop raycasting connection
    if rayhitbox==nil then return end
    rayhitbox:Disconnect()
    rayhitbox = nil
end

hitbox.AttributeChanged:Connect(function(attr)
    if attr == "Activated" and hitbox:GetAttribute("Activated") == true then
        startRaycast()
        print('connected')
    elseif attr == "Activated" and hitbox:GetAttribute("Activated") == false then
        stopRaycast()
        print('disconnected')
    end
end)
solar tinsel
#

Lord have mercy

civic bobcat
#

i just created a function for starting and stopping the runservice connection lol

#

lmk if it works or not

solar tinsel
#

oh

#

functions

#

I never use those

#

unless i'm in python

#

smh vro thx, let me try it out

civic bobcat
#

wow i would think functions are essential for almost everything

solar tinsel
#

I'm just like new to lua

#

so it hasn't set yet

civic bobcat
#

it also helps with readability and scalability. In that specific code you dont have to use functions since you only call them in one spot. But its usually better to

solar tinsel
#

thx man