Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isnt my teleport script working correctly?

this script teleports each player to a random zone every 15 seconds but the output says "bad argument #2 (interval is empty)" why does that happen? Also I think this can teleport 2 or more players to the same zone, is there any way to fix that?

players = game.Players:GetChildren()
spawn1 = workspace.Model1.Baseplate
spawn2 = workspace.Model2.Baseplate
spawn3 = workspace.Model3.Baseplate
spawn4 = workspace.Model4.Baseplate
spawn5 = workspace.Model5.Baseplate
spawn6 = workspace.Model6.Baseplate
spawn7 = workspace.Model7.Baseplate
spawn8 = workspace.Model8.Baseplate
spawn9 = workspace.Model9.Baseplate
spawn10 = workspace.Model10.Baseplate
while true do
	wait(15)
	players[math.random(1, #players)]:MoveTo(spawn1.Position) --bad argument #2 (interval is empty)
	players[math.random(1, #players)]:MoveTo(spawn2.Position)
	players[math.random(1, #players)]:MoveTo(spawn3.Position)
	players[math.random(1, #players)]:MoveTo(spawn4.Position)
	players[math.random(1, #players)]:MoveTo(spawn5.Position)
	players[math.random(1, #players)]:MoveTo(spawn6.Position)
	players[math.random(1, #players)]:MoveTo(spawn7.Position)
	players[math.random(1, #players)]:MoveTo(spawn8.Position)
	players[math.random(1, #players)]:MoveTo(spawn9.Position)
	players[math.random(1, #players)]:MoveTo(spawn10.Position)
end

2 answers

✓ Accepted answer

I think you got the wrong approach here. Currently, I believe your problem is multiple things one it's going to grab a random player but the problem with that is you could get the same player that way. With the :MoveTo() method it attempts to walk a player to that position which isn't relevant for a teleport script.

I would suggest putting your spawns into a folder for organization purposes

I would also suggest before we start to make a table that contains the spawn objects inside like so

local spawnsStorage = game.Workspace.Spawns
local spawnObjects = {
spawnsStorage.Model1.Baseplate,
spawnsStorage.Model2.Baseplate,
spawnsStorage.Model3.Baseplate
-- ...
}

Now we have to do some prep here to go of this type of method we can actually create shallow copies of the spawnObjects table

function copyTable(t)
	local newT = {}
	for i,v in pairs(t) do
		newT[i] = v
	end
	return newT
end

Start by looping through each player

local players = game:GetService("Players")

local seed = Random.new() -- Setup the random seed

while wait(15) do -- Wait 15 seconds before iterating again
	local clonedSpawnObjects = copyTable(spawnObjects)
	for _,player in pairs(players:GetPlayers()) do -- Loop through all players
		if #clonedSpawnObjects > 0 then
			-- Let's get the players character and the humanoids root part
			local char = player.Character
			local hrp = char:FindFirstChild("HumanoidRootPart")
			if char and hrp then -- We're doing a check here to make sure they aren't nil
				local ranNum = seed:NextNumber(1,#clonedSpawnObjects)
				-- Grab a spawnObject from the copied table
				local randomSpawn = clonedSpawnObjects[ranNum]
				-- We're just going to make sure the instance in the table exist
				if randomSpawn then
					-- We are going to set the hrp's CFrame to the spawns CFrame
					-- We're going to add 2 studs to assure they slightly above the spawn
					hrp.CFrame = randomSpawn.CFrame * CFrame.new(0,2,0) 
					-- Remove it so other players don't get the same spawn
					table.remove(clonedSpawnObjects,ranNum)
				end
			end
		end
	end
end

Hope this helps :)

1 comment

0

thanks!

Maybe your models are in workspace.baseplate.model12345...

This discussion is preserved as it was. New answers, comments, and votes are closed.