#Raycasts, again
1 messages · Page 1 of 1 (latest)
is anything printing from print(ray) when you test?
yes
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
the problem is that it doesnt fire appropraitely
like
do you want it to only start the raycasting once the attribute activated is set to true?
yeah
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)
Lord have mercy
i just created a function for starting and stopping the runservice connection lol
lmk if it works or not
oh
functions
I never use those
unless i'm in python
smh vro thx, let me try it out
wow i would think functions are essential for almost everything
exactly
I'm just like new to lua
so it hasn't set yet
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
I'll make sure to use them a lot more
thx man