Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this raycasting script not work online?

Asked by 10 years ago

When I test this when starting a server, it doesnt seem to work. I have NO clue why...

Here's how it works, in starter GUI there is a local script called "Gun" that creates a part, makes workspace its parent, and places the raycasting script (Which does damage) in the part. Keep in mind, "Gun" is a Local Script, and the raycasting script inside called "Raycast" is a normal script.

Here's the script:

local User
local Bullet = script.Parent
local LastPos = Bullet.Position
local RS = game:GetService("RunService").RenderStepped

while true do
local NewRay = Ray.new(LastPos, Bullet.Position - LastPos) --This casts a ray from the LastPos to the Bullet's current position
local H, P = game.Workspace:FindPartOnRay(NewRay, User)
local Human = H and H.Parent and H.Parent:FindFirstChild("Humanoid")
if Human then
local vPlayer = script.Parent.Owner.Value
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = vPlayer
creator_tag.Name = "creator"
creator_tag.Parent = script.Parent
tagHumanoid(Human)
Human:TakeDamage(10)
print("Hit")
untagHumanoid(Human)
script:Destroy()
end
LastPos = Bullet.Position --This resets the LastPos to the bullet's current position
RS:wait() --This waits for the RenderStepped event to fire, which is once every 1/60th of a second
end


function tagHumanoid(humanoid)
    -- todo: make tag expire
    local tag = script.Parent:findFirstChild("creator")
    if tag ~= nil then
        local new_tag = tag:clone()
        new_tag.Parent = humanoid
    end
end


function untagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end 

1 answer

Log in to vote
0
Answered by 10 years ago

It doesn't work because you put the "while true do" loop in the start of the script. I'm going to explain it simple: "While true do" loops make it so the script keeps repeating the same thing, if you use them and there's code after the loops the code won't get read/executed.

Solution:

local User
local Bullet = script.Parent
local LastPos = Bullet.Position
local RS = game:GetService("RunService").RenderStepped

function tagHumanoid(humanoid)
    -- todo: make tag expire
    local tag = script.Parent:findFirstChild("creator")
    if tag ~= nil then
        local new_tag = tag:clone()
        new_tag.Parent = humanoid
    end
end

function untagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end 

--Instead of the start of the script, the loop goes in the end. Everything else stays the same.
while true do
local NewRay = Ray.new(LastPos, Bullet.Position - LastPos) --This casts a ray from the LastPos to the Bullet's current position
local H, P = game.Workspace:FindPartOnRay(NewRay, User)
local Human = H and H.Parent and H.Parent:FindFirstChild("Humanoid")
if Human then
local vPlayer = script.Parent.Owner.Value
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = vPlayer
creator_tag.Name = "creator"
creator_tag.Parent = script.Parent
tagHumanoid(Human)
Human:TakeDamage(10)
print("Hit")
untagHumanoid(Human)
script:Destroy()
end
LastPos = Bullet.Position --This resets the LastPos to the bullet's current position
RS:wait() --This waits for the RenderStepped event to fire, which is once every 1/60th of a second
end
0
I know what a while true loop does. You can list the functions however you want, which is why I CALLED them within the while true loop. I tried removing those functions as well. Still not working. Orlando777 315 — 10y
Ad

Answer this question