[SOLVED] Teleportation works a few times, then stops randomly?

So I’m adding a sword fighting arena to my game, and the teleportation door is in the lobby. When a player touches it, if they have enough wins earned (10) then it teleports them into the arena and sets their AFK to true so if they are in the arena they don’t get teleported into the match. For a little while players with 10+ wins get teleported and it works just fine, but after a certain amount of time I start getting this error (and players can no longer teleport)

It says the math.random interval is empty (arg 2), but it isn’t empty. Any help would be greatly appreciated.

The teleportation script that’s getting the error after a while, but works fine for a little bit at the beginning of the server

local db = false;
local swordSpawns = game.Workspace:WaitForChild("SwordArenaSpawns"):GetChildren()
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if not db then
            db = true
            for _, player in pairs(game.Players:GetPlayers()) do
                if player then
                    if player.Name == hit.Parent.Name then
                        if game:GetService("ServerStorage"):WaitForChild("Players")[player.Name].Wins.Value >= 10 then
                            local playerRoot = hit.Parent:WaitForChild("HumanoidRootPart")
                            local allSwordSpawns = math.random(1, #swordSpawns) -- This is where the error appears
                            local randomSpawn = swordSpawns[allSwordSpawns]
                            if randomSpawn and playerRoot then
                                table.remove(swordSpawns, allSwordSpawns)
                                playerRoot.CFrame = CFrame.new(randomSpawn.Position + Vector3.new(0, 1, 0))
                                wait()
                                game:GetService("ServerStorage"):WaitForChild("Players")[player.Name].isAFK.Value = true
                                wait()
                                game:GetService("ServerScriptService").Remotes.AFKTag:Clone().Parent = playerRoot
                                game.ReplicatedStorage:WaitForChild("ServerClientAFK"):FireClient(player, "toSword")
                                game.ReplicatedStorage:WaitForChild("AFKRound"):FireClient(player, "InRound")
                            end
                        end
                    end
                end
            end
            wait(2)
            db = false
        end
    end
end)

Thanks! Just let me know if you need more information!

Problem:

If there are no sword spawns, then the math.random arguments will be 1 and 0, which will not work because the 2nd argument has to be >= to the first argument in order for math.random to consider it an interval.


Solution:

 local allSwordSpawns = math.random(1, math.max(#swordSpawns, 1))
--replace line 12 with the code above

There are 5 bricks in the sword spawn model, so there are 5 possible spawn locations for the arena. I changed it to your code you provided, this time it still didn’t work, but there was no error in the output anymore, it worked for a little while, and then just stopped again.

I have a model with 5 bricks that acts as spawn points, I want more than 1 spawn point so if multiple players teleport then they don’t just keep piling on top of 1 spot, so I define that model and get it’s children (the 5 bricks) on line 2

local swordSpawns = game.Workspace:WaitForChild("SwordArenaSpawns"):GetChildren()

allSwordSpawns is defined to individualize all of the bricks in the model, and choose 1, and randomSpawn is defined as the chosen spawn brick from inside the swordSpawn model.

allSwordSpawns is only there to choose a random spawn point out of the 5, it’s just how I named it. I guess I don’t need the table.remove() so I’ll get rid of that, but me naming things doesn’t have anything to do with the issue, which is why it works for a random amount of time, then suddenly stops working.

If you are unsure of how it is solved, then I will keep researching, but thank you for your input :slight_smile:

The error still persisted, your code did not solve my problem. I appreciate your feedback though. As for the context, I gave context. I explained the error, provided an output source, and provided the code, as well as explained what the Spawn variables did so you could better understand what was going wrong.

I did some research and figured out what was going wrong. For each time a player teleported, the table.remove() method was Destroying spawn points one by one, removing it solved the error.

Again, I do appreciate your responses and taking the time to write them.