Not sure why it isnt working like it should.Help?
heres the error: 22:40:31.682 - 3 is not a valid member of Model 22:40:31.684 - Script 'Players.Player.PlayerGui.SpawnControl', Line 6 22:40:31.685 - Stack End
local Player = game.Players.LocalPlayer local Map = game.Workspace:WaitForChild('Map') local Spawns = Map:WaitForChild('Spawns') local SpawnsGet = Spawns:GetChildren() local RandomSpawn = math.random(1, #SpawnsGet) local ChoiceSpawn = Spawns[RandomSpawn] game.Workspace.ChildAdded:connect(function(plr) if plr:IsA('Model') and plr:FindFirstChild('Humanoid') then for i,v in pairs(Spawns:GetChildren())do Player.Character:MoveTo(RandomSpawn.Position) end end end)
On line 6, you try to access Spawns
as a table, but it is just referring to a Model in the Workspace. SpawnsGet
is a table. Also, you run the in pairs loop after the script already chose the random spawn. Instead, your script needs to choose a different random spawn each time.
Another thing I changed; I made it so it gets a fresh list of the SpawnLocations each time a player spawns, because I am assuming this game will have multiple maps, and therefore have different sets of spawns. If you don't like this modification, just remove line 13 and change line 8 to:
local Spawns = Map:WaitForChild('Spawns'):GetChildren()
--local Player = game.Players.LocalPlayer -- ^ Remove that, because this is Server code, and should -- therefore go inside a "Script" (not "LocalScript") -- Paste this code in a "Script" inside ServerScriptService local Map = game.Workspace:WaitForChild('Map') local SpawnLocations = Map:WaitForChild('Spawns') game.Workspace.ChildAdded:connect(function(plr) if plr:IsA('Model') and plr:FindFirstChild('Humanoid') then -- Do you have things spawning in your Workspace that are not a Model, but have a Humanoid? local Spawns = SpawnLocations:GetChildren() -- Get an up-to-date list of the Spawns Player.Character:MoveTo(Spawns[math.random(#Spawns)].Position) -- Choose a random Spawn and teleport Player to it end end)