So this seems to have stopped working, it previously would make a part face another part. Any idea what’s wrong here? No errors.
[Edit: It seems that even though it prints out “running” the rest of the script isn’t executing?]
while true do
local p1 = script.Parent
local players = game:GetService("Players"):GetPlayers()
if #players == 0 then return end
print("running")
local p2P = players[math.random(1,#players)]
local p2 = p2P.Character:WaitForChild("Head")
task.wait(1)
for i = 1,100 do
p1.CFrame = p1.CFrame:lerp(CFrame.new(p1.Position, p2.Position), i/100)
wait()
end
end
1 Like
It’s returning when there’s no players.
Since games almost always start up with no players, this immediately exits the loop.
To fix, change the return
to a continue
and move the task.wait(1)
to the top of the loop (so it waits regardless of the player count).
You should also not assume the player always has a .Character
. You also don’t need to WaitForChild
, since the loop runs so often. Change the WaitForChild
line to something like this:
local character = p2P.Character
if not character then continue end
local p2 = character:FindFirstChild("Head")
if not p2 then continue end
Yea but it’s printing “running” which happens after the return, it’s in a loop so should it not continue looping until players are loaded?
What is continue
is that something new?
continue
is used in loops to go to the next iteration or goes back to the start of the loop.
while true do
wait(1)
local random = math.random
local condition = random > 0.5
if condition then
print("returns to the start")
continue
end
print("reached the end")
end
Every second, the code makes a random number. If the number is more than 0.5 (which is 50%), then the loop would print whatever that is and will continue
, meaning the “reached the end” will not print. If the number is less than 0.5, then it will not continue
and it will print “reached the end”.
break
is the same, except it completely stops or “breaks” the loop. But that is out of the question.
Good point. It could be that a player exists but their Character
was nil
on the local p2 = ...
line. I’m not sure though.
Anyways, I tested the code with the changes I suggested and it is working for me.
hmm I didn’t get any errors though for character. I’ll try your code.