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

Map loader doesn't do anything? Any sugestions?

Ok so I have a map loader in my game and instead of doing anything, yes it does change the status bar but it doesn't move the map from replicated storage into workspace and teleports anyone, I really don't know what to say. Heres the game: https://web.roblox.com/games/5035614876/Skirmish Heres my code.

local maps = game.ReplicatedStorage.Maps:GetChildren()

local allplayers = game.Players:GetChildren()

local allcharacter = allplayers.Character

while true do
	game.ReplicatedStorage.status.Value = "Intermission (20 Seconds)"
	wait (18)
	game.ReplicatedStorage.status.Value = "Selecting map, Hold On!"
	wait (2)
	game.ReplicatedStorage.Maps.Map1:Clone() .Parent = game.Workspace
	game.ReplicatedStorage.status.Value = "Map selected: Lab"
	wait (1)
	local allspawns = game.Workspace.Map1.Spawns:GetChildren()
	local randomspawn = allspawns[math.random(1, #allspawns)]
	allcharacter.HumanoidRootPart.CFrame = randomspawn.CFrame
	wait (2)
	game.ReplicatedStorage.status.Value = "Get Ready!"
	wait(5)
	game.ReplicatedStorage.status.Value = "Go!"
	game.ReplicatedStorage.Knife:Clone() .Parent = allplayers.Backpack
	game.ReplicatedStorage.Revolver:Clone() .Parent = allplayers.Backpack
	wait (180)
	allcharacter.Humanoid.Health = 0
	wait (8)
	game.Workspace.Map1:Destroy()
end

Edit: I didn't do a random map loader because I had the same problem but I might change it soon

2 answers

✓ Accepted answer

Hello.


Problem:

  • GetChildren() returns a table, not the players in-game.

Solution:

  • Use an in pairs loop. It will loop through the table of players and teleport them all. If you didn't do this, you'd be adding a value to the table called character or HumanoidRootPart.

Recommendations:

  • Use GetPlayers() instead of GetChildren() as some people might use Players to store things (don't use it to store things though).

  • Put the allplayers variable inside the while true do loop as the players can change (leaving and joining). If you put it outside the loop, no new players who joined would get added to the table.


Fixed Script:

local maps = game.ReplicatedStorage.Maps:GetChildren()

while true do
	local allplayers = game.Players:GetPlayers()

    game.ReplicatedStorage.status.Value = "Intermission (20 Seconds)"
    wait (18)
    game.ReplicatedStorage.status.Value = "Selecting map, Hold On!"
    wait (2)
    game.ReplicatedStorage.Maps.Map1:Clone() .Parent = game.Workspace
    game.ReplicatedStorage.status.Value = "Map selected: Lab"
    wait (1)
    local allspawns = game.Workspace.Map1.Spawns:GetChildren()
    local randomspawn = allspawns[math.random(1, #allspawns)]
	for i, v in pairs(allplayers) do 
   		v.Character.HumanoidRootPart.CFrame = randomspawn.CFrame
	end
    wait (2)
    game.ReplicatedStorage.status.Value = "Get Ready!"
    wait(5)
    game.ReplicatedStorage.status.Value = "Go!"
    game.ReplicatedStorage.Knife:Clone() .Parent = allplayers.Backpack
    game.ReplicatedStorage.Revolver:Clone() .Parent = allplayers.Backpack
    wait (180)
	for i, v in pairs(allplayers) do
    		v.Character.Humanoid.Health = 0
	end
    wait (8)
    game.Workspace.Map1:Destroy()
end

4 comments

0

Thank su

0

You're setting the allPlayers variable once, at the beginning of the game. What if in the 20 seconds before the map is chosen more people join or leave? You still won't have the updated version of the allPlayers variable. You use the same random spawn for every player? They'd all teleport to the same spawn at the same time. May be a bit of a strange experience.

0

You're assuming that their Character and HumanoidRootPart have loaded on line 16. You also put ".Backpack" after the allPlayers variable... despite the fact that it is a table and you'd have to iterate through the table to get each individual player's backpack.

0

Seriously, this is rushed like crazy. Take your time and don't rush answers.

At the time that you assign the allplayers variable, it's possible that not all the players have loaded in the game. You need to set the variable right before you need to use it so you can get the most accurate list of players.

Also, you can't just add .Character to the end of a list of players. You'd have to use a for loop to iterate through each player in order to move all of them to the map.

local maps = game.ReplicatedStorage:WaitForChild("Maps"):GetChildren()

_G.playersAlive = {}

while true do
	_G.playersAlive = {}
	game.ReplicatedStorage.status.Value = "Intermission (20 Seconds)"
	wait(18)
	game.ReplicatedStorage.status.Value = "Selecting map, Hold On!"
	wait(2)
	game.ReplicatedStorage.Maps.Map1:Clone().Parent = game.Workspace
	game.ReplicatedStorage.status.Value = "Map selected: Lab"
	wait(1)
	local allspawns = game.Workspace.Map1.Spawns:GetChildren()
	
	local allPlayers = game.Players:GetPlayers()
	for i, v in pairs(allPlayers) do
		local character = v.Character
		if character then
			local humanoid = character:FindFirstChild("Humanoid")
			if humanoid then
				character:MoveTo(allspawns[i].Position)
				table.insert(_G.playersAlive, v)
			end
		end
	end

	wait (2)
	game.ReplicatedStorage.status.Value = "Get Ready!"
	wait(5)
	game.ReplicatedStorage.status.Value = "Go!"
	
	for i, v in pairs(_G.playersAlive) do
		local backpack = v:WaitForChild("Backpack")
		local knife = game.ReplicatedStorage:WaitForChild("Knife"):Clone()
		knife.Parent = v.Backpack

		local revolver = game.ReplicatedStorage:WaitForChild("Revolver"):Clone()
		revolver.Parent = v.Backpack
	end
	wait (180)
	
	for i, v in pairs(alivePlayers) do
		local chr = v.Character
		if chr then
			local humanoid = chr:FindFirstChild("Humanoid")
			if humanoid then
				humanoid.Health = 0
			end
		end
	end
	wait(8)
	game.Workspace.Map1:Destroy()
end

I added in the for loops, added some WaitForChild functions on some stuff(because you can't ever assume that an instance has loaded), and I also added a global table that keeps track of the players that are currently in the game. The reason I did this was because if someone dies you should know. And the reason I made the table a global variable(meaning it can be accessed from other scripts) is because I want to recommend putting this script in your game:

game.Workspace.ChildAdded:Connect(function(child)
	local plr = game.Players:GetPlayerFromCharacter(child)
	if plr then
		local humanoid = child:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			if table.find(_G.alivePlayers, plr) then
				table.remove(_G.alivePlayers, table.find(_G.alivePlayers))
			end
		end)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local index = table.find(_G.alivePlayers, plr)
	if index then
		table.remove(_G.alivePlayers, index)
	end
end)

It listens for when a child is added to the workspace, and determines whether or not that child is the character of a player. If it is, listen for when its humanoid dies, and when it does, if it is inside of the _G.alivePlayers global table, then remove the player them from it. It also accounts for players leaving the game.

I know this is much more than you requested, but I seriously couldn't help myself. I saw that you weren't keeping track of the players and just couldn't ignore that.

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