Hi I made this raycast cycle through all players characters in a game and shoot their heads, but when there is a wall in the way the raycast ignores this and goes through it getting to its destination anyway. How can I make the raycast stop if there is something in the way? Thanks.
local part = script.Parent
while wait(1) do
for _,v in pairs(game.Players:GetPlayers()) do
local character = v.Character
if character then
local myFirstRay = Ray.new(part.Position, (part.Position-character.Head.Position))
local hit, position = game.Workspace:FindPartOnRay(myFirstRay, character)
if hit then
print "Ray intersected another part!"
local laser = Instance.new("Part")
laser.Parent = workspace
laser.CanCollide = false
laser.Anchored = true
local dist = (part.Position-character.Head.Position).magnitude
laser.CFrame = CFrame.new(part.Position, character.Head.Position)*CFrame.new(0,0,-dist/2)
laser.Size = Vector3.new(0.1,0.1,dist)
game.Debris:AddItem(laser,0.2)
else
print "No part found"
end
end
end
end