I'm trying to make a game script where it picks a random map, creates a message saying the map name, then clones the map to Workspace, deletes the message and respawns all players. But for some reason it won't work I get this output error: attempt to get length of upvalue 'MapDir' (a userdata value). Can anyone tell me what I did wrong?
local MapDir = game.ServerStorage.Maps local PlayerDir = game.Players local WaitTime = 10 -- In seconds local RoundTime = 600 -- In seconds local Started = false function GameStart() MapDir:GetChildren() for i = 1, #MapDir do local m = Instance.new('Message', workspace) m.Text = "Map chosen: "..MapDir[i].Name.." by:"..MapDir[i].CreatedBy.Value.."" wait(WaitTime) MapDir[i]:Clone().Parent = workspace.MapHolder PlayerDir:GetChildren() for i = 1,#PlayerDir do PlayerDir[i]:LoadCharacter() Started = true m:remove() end end end GameStart()
local MapDir = game.ServerStorage.Maps local PlayerDir = game.Players local WaitTime = 10 -- In seconds local RoundTime = 600 -- In seconds local Started = false function GameStart() MapDir:GetChildren() for i = 1, #MapDir do local m = Instance.new('Message', workspace) m.Text = "Map chosen: "..MapDir[i].Name.." by: "..MapDir[i].CreatedBy.Value.."" wait(WaitTime) MapDir[i]:Clone().Parent = workspace.MapHolder PlayerDir:GetChildren() for i = 1,#PlayerDir do PlayerDir[i]:LoadCharacter() Started = true m:remove() end end end GameStart()
--[[ Line 8 ]] MapDir:GetChildren() --[[ Line 14 ]] PlayerDir:GetChildren()
You're on the right track, but this won't work because that's all you did. You didn't set it to a variable or anything. Line 8 will be fixed in the 3rd bullet, but line 14 is the one that needs fixing right here, because the amount of players in the server will change because of players leaving/entering.
--[[ Line 14 ]] local CurrPlayerDir = PlayerDir:GetChildren() -- Curr = "Current", or updated --[[ Line 15-19 ]] for i = 1, #CurrPlayerDir do CurrPlayerDir[i]:LoadCharacter() Started = true m:Destroy() -- Using the ":Destroy()" method is recommended over ":Remove()". end
MapDir
is set to the WRONG data type. What the "for" loop on line 9-20 is intended to do is index a table. But MapDir
is not a table, it's something else. On line 8, It's trying to get the length of the container of the maps, not regarding the actual THINGS in the container.--[[ Line 1 ]] local MapDir = game.ServerStorage.Maps:GetChildren() -- If you did this, then delete line 8.
-- Numeric "for" loop TABLE = {} for i = 1, #TABLE do TABLE[] end
-- Generic "for" loop TABLE = {} for i, v in pairs(TABLE) do v end -- Where "v" means value.
With the generic "for" loop, you get to type a lot less than the former, because the variable for value
is a lot shorter than indexing a table (table[key]
). But that depends on the purpose of the "for" loop. The generic "for" loop is good for tables.
hashtag
hyphen under the line
test
nothing