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
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