This script is supposed to teleport a player to a spawn point. the print(AvailableSpawnPoints[1]) prints SpawnPoint 1 yet the player doesn’t even get teleported to SpawnPoint1. I’ve been stuck on this problem for over 5 hours now. What could be causing this?
if character then
character.HumanoidRootPart.Position = AvailableSpawnPoints[1].Position
print(AvailableSpawnPoints[1])
table.remove(AvailableSpawnPoints,1)
player.playerAmount.Value = 0
game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
end
Use :MoveTo() on the character model instead of setting its position
if character then
character:MoveTo(AvailableSpawnPoints[1].Position)
table.remove(AvailableSpawnPoints, 1)
player.playerAmount.Value = 0
game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
end
if #plrs == 1 then
local player
for i, v in pairs(plrs) do
player = v
end
if player then
character = player.Character
if character then
character:MoveTo(AvailableSpawnPoints[1].Position)
print(AvailableSpawnPoints[1])
table.remove(AvailableSpawnPoints,1)
player.playerAmount.Value = 0
game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
end
end
end
If the print(AvailableSpawnPoints[1]) => SpawnPoint 1 then I’d assume that your AvailableSpawnPoints would be strings. If this was the case, you’d either want to change your AvailableSpawnPoints table to the objects of the SpawnPoints (Example 1) or you would want to find the spawnpoint somewhere in the workspace and teleport the player that way (Example 2).
Example 1 — Objects In AvailableSpawnPoints
--Example Table Objects
local AvailableSpawnPoints = {}
for i, spawn in pairs(game.Workspace.SpawnPointsFolder) do
table.insert(AvailableSpawnPoints, spawn)
end
-- Basically I am looping through all of your spawnpoints and inserting the object
--(this is assuming your spawns are in a single folder with nothing else)
if #plrs == 1 then
local player
for i, v in pairs(plrs) do
player = v
end
if player then
character = player.Character
if character then
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
print(AvailableSpawnPoints[1].Name) --- Should print "SpawnPoint 1"
table.remove(AvailableSpawnPoints,1)
player.playerAmount.Value = 0
game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
end
end
end
Example 2 — Strings In AvailableSpawnPoints
--Example Table Objects
local AvailableSpawnPoints = {}
for i, spawn in pairs(game.Workspace.SpawnPointsFolder) do
table.insert(AvailableSpawnPoints, spawn.Name)
end
-- Basically I am looping through all of your spawnpoints and inserting the names
--(this is assuming your spawns are in a single folder with nothing else)
if #plrs == 1 then
local player
for i, v in pairs(plrs) do
player = v
end
if player then
character = player.Character
if character then
for i, spawn in pairs(game.Workspace.SpawnPointsFolder) do
if spawn.Name == AvailableSpawnPoints[1] then
character:FindFirstChild("HumanoidRootPart").CFrame = spawn.CFrame
end
end
-- You want to find the spawn with the same name as AvailableSpawnPoints[1]
print(AvailableSpawnPoints[1]) --- Should print "SpawnPoint 1"
table.remove(AvailableSpawnPoints,1)
player.playerAmount.Value = 0
game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
end
end
end
If you have any questions ask, and I am writing this assuming that your current table is in Strings as said before because of the print statement you mentioned in the OP. (Also make sure your plrs table is Objects and not Strings or it wouldn’t work anyways)
So I was trying to do example 1 and changed my script to this. (It loops through a table of 5 players. It was working fine the first 2 times I tried it, but then it started spawning in the wrong spot again. This only happens to the last player in the table.
local SpawnPoints = ClonedArena:FindFirstChild("SpawnPoints")
local AvailableSpawnPoints = {}
for i, spawns in pairs(SpawnPoints:GetChildren()) do
table.insert(AvailableSpawnPoints, spawns)
end
repeat
print(#plrs)
local player = plrs[math.random(1,#plrs)]
for x, plr in pairs(plrs) do
if plr == player then
table.remove(plrs,x)
end
end
if player then
character = player.Character
if character then
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
print(AvailableSpawnPoints[1].Name)
table.remove(AvailableSpawnPoints,1)
player.playerAmount.Value = 0
game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
end
end
until #plrs == 0
First, I would guess there are, but are there enough spawns for each player?
Second, is the print(#plrs) printing correctly? (5>4>3 etc).
Third, you could also add a print for #AvailableSpawnPoints.
Last, does the ASP table reset after a round or whatever or do the spawn points keep getting removed and never added back?
Edit:
What happens to the last player when it messes up specifically?
You also don’t need the for x, plr loop. You could simply do:
local chosen = math.random(1, #plrs)
local player = plrs[chosen]
table.remove(plrs, chosen)
The player either doesn’t get teleported at all or they get teleported to the default spawn point (the place where all players spawn when they join the game) However the rest of the script still runs for that player because the WaitScreenEndBang does get fired to them.