So I have a script set up for teleporting a player after the map has loaded, except they won’t teleport. If there is any way you can help me fix this that would be great, I have nothing that comes up in the output after it goes through the for loop sadly, so something is wrong with the loop itself I believe. Could this be a bug, or do I just not have something made correctly? Here is part of my script:
-- define
local currentmap = workspace:WaitForChild("CurrentMap")
local chosenmap = script:WaitForChild("ChosenMap") -- this is set when the map has been chosen
local players = game.Players:GetPlayers()
-- teleport players
function teleportPlayers()
for i,v in pairs(players) do
v.Character.HumanoidRootPart.CFrame = currentmap:FindFirstChild(chosenmap.Value).Spawn.CFrame * CFrame.new(math.random(5,10),0,math.random(5,10))
end
end
The issue is probably because of the local players = game.Players:GetPlayers() line, because the table assigned to this variable will always be constant, unless you’re actually updating it somewhere else, but in this excerpt, it’s not being updated and also perform checks to make sure that their character exists so the script doesn’t throw any errors
You should define a new variable when the function is called:
function teleportPlayers()
local players = game.Players:GetPlayers()
for _,v in pairs(players) do
if v.Character then
-- adjust HRP CFrame here
end
end
end
no, none of this is a local script, I am using a (Server)Script to call my functions. That wouldn’t make sense if I did this in a local script and yes I am using filtering enabled.
oh yeah I could do that, but the problem was the server not constantly assigning the “players” variable, as @Raretendoblox has said. I just tested this out, but thanks for the help, I appreciate it!