So this part of code in my script should teleport the player once the round ends but it does everything else instead of teleporting the character
for _, v in pairs(playerService:GetChildren()) do
wait(1)
v:FindFirstChild("Data").InRound.Value = false
local char = v.Character
char.PrimaryPart.CFrame = lobbySpawn.CFrame
table.clear(playersInGame)
char:FindFirstChild("Hitbox"):Destroy()
end
for _, v in pairs(playerService:GetChildren()) do
wait(1)
v:FindFirstChild("Data").InRound.Value = false
local char = v.Character
if char then
local primaryPart = char.PrimaryPart
if primaryPart then
if lobbySpawn then
primaryPart.CFrame = lobbySpawn.CFrame
else
warn("Spawn location 'lobbySpawn' is not defined.")
end
else
warn("PrimaryPart is missing in the character.")
end
-- Clear any existing data in the 'playersInGame' table
table.clear(playersInGame)
local hitbox = char:FindFirstChild("Hitbox")
if hitbox then
hitbox:Destroy()
else
warn("Hitbox is missing in the character.")
end
else
warn("Character is missing for player:", v.Name)
end
end
it should be working or else the output will identify your problem,reply me after you have tested it
The code you provided seems to be mostly correct. However, there is one line that could potentially cause an issue. The line table.clear(playersInGame) clears the playersInGame table, which might not be the intended behavior for teleporting the player. If you want to clear the table after teleporting the player, it should be moved outside the loop. Here’s the modified code:
for _, v in pairs(playerService:GetChildren()) do
wait(1)
v:FindFirstChild("Data").InRound.Value = false
local char = v.Character
char.PrimaryPart.CFrame = lobbySpawn.CFrame
char:FindFirstChild("Hitbox"):Destroy()
end
table.clear(playersInGame)
Make sure to check that the lobbySpawn variable is correctly defined and points to the desired spawn location. Also, ensure that the Hitbox part exists in the character and is correctly named. With these adjustments, the player’s character should be teleported to the lobby spawn point once the round ends.
I tried everyone’s ideas but still wont teleport the player. I commented out some parts of the script and found out that the HitBox:Destroy() was the problem. I think the main problem of the script is that the Hitbox has a WeldConstraint inside of it and without the weld everything works normally. I dont know how to get past this since I need the weld to connect the PrimaryPart with the Hitbox
Tested out some variations from you guys and just had to add a little wait here
I suspect the Destroy function and the CFrame changing changes at the same time and the system is confused?
for _, v in pairs(playerService:GetChildren()) do
wait(1)
v:FindFirstChild("Data").InRound.Value = false
local char = v.Character
char.PrimaryPart.CFrame = lobbySpawn.CFrame
wait() --wait here to fix the script
local hitbox = char:FindFirstChild("Hitbox")
if hitbox then
hitbox:Destroy()
else
warn("Hitbox is missing")
end
end