Hello! I was making a round system for my game but I can’t get players to teleport to their designated spot.
It used to work until I made the characters refresh each time a round starts/finishes.
I tried to use :WaitForChild() to no avail. However when I try to do it in the developer console it works?
Heres my code:
for count = 1, #BuildingZones do
local Players = PlayersService:GetPlayers()
if Players[count] ~= nil then
BuildingZones[count]:SetAttribute("userID", Players[count].UserId)
table.insert(connections, Players[count].CharacterAdded:Connect(function()
-- TP current Player to current Zone
Players[count].Character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(BuildingZones[count].Position)
-- Clones the tools into the current Player's backpack.
for _, tool in pairs(Tools.Building:GetChildren()) do
local clonedTool = tool:Clone()
clonedTool.Parent = Players[count].Backpack
end
end))
Players[count]:LoadCharacter()
end
end
(I store all my connections in a table to be able to :Disconnect() them all more efficiently.)
Ideally, you would be setting the player’s cframe on the client but you can just add a task.wait() before you set the cframe on server. (Equivalent to Heartbeat:Wait())
local connections = {}
for count = 1, #BuildingZones do
local Players = PlayersService:GetPlayers()
if Players[count] ~= nil then
BuildingZones[count]:SetAttribute("userID", Players[count].UserId)
local function onCharacterAppearanceLoaded(player)
-- Disconnect the CharacterAppearanceLoaded event once it's fired to avoid unnecessary re-teleporting.
connections[count]:Disconnect()
local character = player.Character
if character then
character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(BuildingZones[count].Position)
-- Clones the tools into the current Player's backpack.
for _, tool in pairs(Tools.Building:GetChildren()) do
local clonedTool = tool:Clone()
clonedTool.Parent = player.Backpack
end
end
end
connections[count] = Players[count].CharacterAppearanceLoaded:Connect(function()
onCharacterAppearanceLoaded(Players[count])
end)
Players[count]:LoadCharacter()
end
end
How exactly does the player not able to teleport? Try teleporting them, but have a wait line to see if the teleport work before the character is loaded.
Do you mean teleporting? Also why wouldn’t it be a bug it only happens when I skip a heartbeat.
Anyways here my code if you meant teleport:
for count = 1, #BuildingZones do
local Players = PlayersService:GetPlayers()
if Players[count] ~= nil then
BuildingZones[count]:SetAttribute("userID", Players[count].UserId)
table.insert(connections, Players[count].CharacterAdded:Connect(function()
task.wait() -- 2 times to bypass that weird glitch
task.wait()
-- Clones the tools into the current Player's backpack.
for _, tool in pairs(Tools.Building:GetChildren()) do
local clonedTool = tool:Clone()
clonedTool.Parent = Players[count].Backpack
end
Players[count].Character:WaitForChild("HumanoidRootPart").Position = BuildingZones[count].Position
end))
Players[count]:LoadCharacter()
end
end