I’m trying to make a script that teleports players to a certain part, but it only works once and then stops working. I’ve tried searching it up on youtube but got nothing.
local RS = game.ReplicatedStorage
local Maps = RS.Maps
local CountDownTime = 5 --Time until a round starts
local RoundEndTime = 5 -- Time for a round
local TimeValue = script.TimeValue
local Mapstoload = Maps:GetChildren()
local randomMap
local players = game.Players:GetChildren()
function roundIntermission()
TimeValue.Value = CountDownTime
for count = 1,CountDownTime do
TimeValue.Value = TimeValue.Value - 1
wait(1)
end
end
function getRandomMap()
randomMap = Mapstoload[math.random(1, #Mapstoload)]
randomMap.Parent = game.Workspace
for i,v in pairs(players) do
players.Character.HumanoidRootPart.CFrame = randomMap.spawnPart.CFrame
end
end
function RemoveMap()
randomMap.Parent = RS
for i,v in pairs(players) do
players.Character.HumanoidRootPart.CFrame = game.Workspace.LobbySpawn.CFrame
end
end
function roundTime()
TimeValue.Value = RoundEndTime
for count2 = 1, RoundEndTime do
TimeValue.Value = TimeValue.Value - 1
wait(1)
end
end
while wait() do
roundIntermission()
getRandomMap()
roundTime()
RemoveMap()
end
Try to use Players:GetPlayers() over GetChildren()
for i,v in pairs(players) do
players.Character.HumanoidRootPart.CFrame = game.Workspace.LobbySpawn.CFrame
end
Players is what your looping through. If you do this, I’m certain an error will come up. v is one of the objects in the players table, so instead change the v ’s CFrame.
Hopefully this helped a bit.
Once again, replace players.Character with v.Character.
Players is the table being looped through, and if you try to access “character” which obviously doesn’t exist it will probably return referencing a entry that doesnt exist.
You shouldn’t do this in a variable outside of a function. This is because you’re getting the list of players at that time, meaning if a player leaves or joins it doesn’t update. You should do that every time the function is called.
function getRandomMap()
randomMap = Mapstoload[math.random(1, #Mapstoload)]
randomMap.Parent = game.Workspace
local players = game.Players:GetPlayers()
for i,v in pairs(players) do
v.Character.HumanoidRootPart.CFrame = randomMap.spawnPart.CFrame
end
end
function RemoveMap()
randomMap.Parent = RS
local players = game.Players:GetPlayers()
for i,v in pairs(players) do
v.Character.HumanoidRootPart.CFrame = game.Workspace.LobbySpawn.CFrame
end
end