Hello! I’m working on a Splinter Cell type game and I have this light part that will look for the player and if the raycast hits will bring up their “hidden” level. However I’m having trouble with the loop that checks if the player is in the light, it’s just not repeating!
repeat
wait(.2)
for i,v in pairs(game.Players:GetChildren()) do
local char = v.CharacterAdded:Wait() or v.Character
local rayorig = script.Parent.Position
local raydes = char:WaitForChild("Head").Position
local rayresult = workspace:Raycast(rayorig,raydes)
print(rayresult.Instance.Name)
if rayresult.Instance.Name == "Head" then
print("yep")
end
end
until false
this one breaks it all, you have to just use v.Character, no need to wait for one
Also, you are using raycast wrong, it has 2 main arguments Origin and Direction
you are missusing the Direction argument just as destination, which is wrong
The script is not looping because of this line v.CharacterAdded:Wait() or v.Character. It will wait until the CharacterAdded event is fired, which only happens whenever the character (re)spawns. So, when looping through the players, if the character is already alive, it will yield infinitely. At least that is my theory.