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

Why is this value being called as "nil" even though I defined it?

Hello, I made a script where if the round ends teleports all the players to the lobby spawns. I defined everything but can't seem to find an issue. It gives me and error.

error:

16:06:24.217 - ServerScriptService.MainScript:364: attempt to get length of global 'lobbyspawns' (a nil value)

here is the section of script that matters

	-- Teleport players back to lobby and remove tools.
	lobbyspawns = {}
	for _, v in pairs(game.Workspace:WaitForChild("Lobby"):GetChildren()) do
		if v ~= nil and v.Name == "SpawnLocation" then
			table.insert(lobbyspawns, v)
		end
		print (#lobbyspawns)
	end
end
for _, player in pairs(aliveplayers)do
	if player ~= nil then
		awardpoints(player, 2)
		if player.Character ~= nil then
			local humanoid = player.Character:FindFirstChild("Humanoid")
			if humanoid ~= nil then
				humanoid:UnequipTools()
			end
		end
		local randomspawn = lobbyspawns[math.random(1, #lobbyspawns)]
		print(#lobbyspawns)
		player.Character:MoveTo(randomspawn.Position)
		local backpack = player:FindFirstChild("Backpack")-- Remove tools.
		if backpack ~= nil then
			backpack:ClearAllChildren()
		end
	end
end

the lobby name is "Lobby" and the children are names SpawnLocation. however when I try to teleport them it doesn't work and a got that error.

How can I get this to work?

4 comments

1

Can you tell us which line it is?

0

yeah, sorry, its line 19

0

Can you include enough of your script that we actually know everything that's going on? The context of the chunk of code matters a lot.

0

its a pretty big amount of code, i figured all thats needed would be this since this is where the issues is and where the issue is called.

2 answers

Its most likely because of choosing random spawns. Maybe, if you assign everyone numbers (added when they join, and when someone leaves the server other numbers change to correct themselves, Example: player joins, is first in server so number is 1, the next player joins with 2, and another player joins, with the number 3. the player with number 2 leaves. player 3 will become number 2.) Assign all the number to different spawns and that might work.

4 comments

0

I wouldn't know if this is relevant due to the fact, that this map hasn't been player publiclly and has only been tested in local servers, I have a script that teleports players to a random map spawn, but im having issues drawing them back. Since I tested on local servers, none of the players left.

0

ohhh

0

so you can get them to spawn but your having trouble spawning them back... an easy way would be to call the :loadcharacter function on every player, because if you do that they get sent back to the lobby. I sometimes misread things so i might be getting confused here

0

Well basically I'm using cframes of the spawns t teleport to the maps with a vector y of 3 so that I dont spawn in the ground. I then used the move to function to put players back in the lobby, sense I used the move to function, the players can stack on each other. I could simply just kill all the players, and they would go back to spawn but what fun would that be, it would seem un-professional.

Your issue resides on that very first line. Unfortunately, you don't give enough information to know what the issue is exactly (It resides in the code before what is shown) however if the lobbyspawns is within a while loop, for ... in loop or if statement, you'll need to check your condition because it's not evaluating to true and therefore your code is skipping the assignment of lobbyspawns

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