Random spawning not working efficiently?

I’m working on a random spawning script which moves player to a random part called “SpawnPad”

local SpawnPads = {}

for i, child in pairs (workspace.Map.Map:GetChildren()) do
	if child.Name == "SpawnPad" then
		table.insert(SpawnPads,child)
	end
end

rp.Respawn.OnServerEvent:Connect(function(plr)
	...

	plr:LoadCharacter()

		plr.Character:MoveTo(SpawnPads[math.random(1,#SpawnPads)].Position)
		wait(.5)
		plr.Character.Humanoid.WalkSpeed = 17
		
	if plr.Character:FindFirstChildOfClass("ForceField") then
		game:GetService("Debris"):AddItem(plr.Character.ForceField,3)
	end

	...
	
end)

But I found that this is not really consistent, I found players sometimes did not spawn and got stuck, there are no errors in output. Is there a better way to perform this or resolve it?

2 Likes

Can you specify by what you mean players got stuck?

Also, just to make sure, you have PlayerAutoLoads under Players turned off, right?
And, is there any particular reason you have the client calling the Respawn event?


local SpawnPads = {}

for _, spawnPad in pairs(workspace.Map.Map:GetChildren()) do
    if spawnPad.Name == 'SpawnPad' then
        table.insert(SpawnPads, spawnPad)
    end
end

rp.Respawn.OnServerEvent:Connect(function(player)
    player:LoadCharacter()

    local character = player.Character or player.CharacterAdded:Wait()

    character:MoveTo(SpawnPads[Random.new():NextInteger(1, #SpawnPads)].Position)

    wait(.5)

    character.Humanoid.WalkSpeed = 17

    if character:FindFirstChild('ForceField') then
        game:GetService('Debris'):AddItem(character.ForceField, 3)
    end
end)

Tell me if this code makes any difference

I’ll try the code and see the result, by player getting stuck, I mean they will spawn in a SpawnLocation and does not move to a SpadnPad. Their WalkSpeed isn’t 17 and they have ForceField.

I don’t see it glitches anymore, thanks!

1 Like