Generating a random number on join

  1. What is supposed to happen: Player joins the lobby, and is assigned a random number from 1 to 7.

  2. What really happens: nothing happens

  3. What solutions have you tried so far? I couldn’t find much that helped me on the devforum.

this is the script I made to assign the number on connect:

game.Players:Connect()
local Players = game.GetService("Players")
local function PlayerAdded(plr)
	local mapspawn = Instance.new("Folder")
	mapspawn.Name = "spawnpoint"
	local spawnpoint = Instance.new("NumberValue")
	spawnpoint.Name = "point"
	spawnpoint.Parent = spawnpoint
	spawnpoint.Value = math.random(1,7)
	spawnpoint.Parent = plr
end

And this is the script I use to teleport them to the spawns:


		for i, plr in pairs(game.Players:GetChildren()) do
			
			local char = plr.Character
			local humanRoot = char:WaitForChild("HumanoidRootPart")
			
			if	plr.spawnpoint.point == 1 then  humanRoot.CFrame = game.Workspace.mapspawn.spawn1.CFrame 
			else if	plr.spawnpoint.point == 2 then humanRoot.CFrame = game.Workspace.mapspawn.spawn2.CFrame 
				else if	plr.spawnpoint.point == 3 then  humanRoot.CFrame = game.Workspace.mapspawn.spawn3.CFrame 
					else if plr.spawnpoint.point == 4 then  humanRoot.CFrame = game.Workspace.mapspawn.spawn4.CFrame 
						else if	plr.spawnpoint.point == 5 then  humanRoot.CFrame = game.Workspace.mapspawn.spawn5.CFrame 
							else if	plr.spawnpoint.point == 6 then  humanRoot.CFrame = game.Workspace.mapspawn.spawn6.CFrame 
								else if	plr.spawnpoint.point == 7 then  humanRoot.CFrame = game.Workspace.mapspawn.spawn7.CFrame

								
									end
								end
							end
						end
					end
				end
			end
			
1 Like

This is the script I made to assign the number on connect:

If that’s the full script then nothing happens because the PlayerAdded function is not being connected. Here’s an example of how you could connect it:

game.Players:Connect()
local Players = game.GetService("Players")
local function PlayerAdded(plr)
	local mapspawn = Instance.new("Folder")
	mapspawn.Name = "spawnpoint"
	local spawnpoint = Instance.new("NumberValue")
	spawnpoint.Name = "point"
	spawnpoint.Parent = spawnpoint
	spawnpoint.Value = math.random(1,7)
	spawnpoint.Parent = plr
end

game.Players.PlayerAdded:Connect(PlayerAdded)-- This connects to the function when a player is added

You might aswell clean up a bit the teleport code to make it more readable and fix this issue, you maybe want to use a list to make the system simpler

local Players = game:GetService("Players")
local mapspawns = workspace.mapspawn

for _, plr in Players:GetPlayers() do -- Loop over the player list
        local char = plr.Character
        local root = char:WaitForChild("HumanoidRootPart") -- Waiting for rootparts will delay other player teleports, might want to make each iteration a different thread
        local spawnpoint = plr.spawnpoint.point.Value -- Add value, it is important!
        local spawn = mapspawns:FindFirstChild("spawn"..tostring(spawnpoint)) -- Check if there is an existing spawnpoint (spawn1, spawn2, ...)
        if not spawn then continue end -- if there is no spawn go for the next player, might wanna change this logic
        root.CFrame = spawn.CFrame -- teleport player!
end

this fixes the original problem! thanks

i also typed part of the script wrong, it should be game:GetService(“Players”) instead of game.GetService(“Players”)

This is part of the teleport script, with your edit

there isn’t any red underlined errors, and I don’t know how to format this script well.

inRound.Changed:Connect(function()
    
    if inRound.Value == true then
        
        for i, plr in pairs(game.Players:GetChildren()) do

            local char = plr.Character
            local humanRoot = char:WaitForChild("HumanoidRootPart")

            for _, plr in Players:GetPlayers() do -- Loop over the player list
                local char = plr.Character
                local root = char:WaitForChild("HumanoidRootPart") -- Waiting for rootparts will delay other player teleports, might want to make each iteration a different thread
                local spawnpoint = plr.spawnpoint.point.Value -- Add value, it is important!
                local spawn = game.Workspace.mapspawn:FindFirstChild("spawn"..tostring(spawnpoint)) -- Check if there is an existing spawnpoint (spawn1, spawn2, ...)
                if not spawn then continue end -- if there is no spawn go for the next player, might wanna change this logic
                root.CFrame = spawn.CFrame -- teleport player!
            end

            
            plr.Team = playingTeam
            
            char:WaitForChild("Humanoid").Died:Connect(function()
                
                plr.Team = lobbyTeam
                
                
            end)
            
        end    
        
    else

this is my original teleport line, when there was barely a round system

humanRoot.CFrame = game.Workspace.mapspawn.spawn[math.random(1,7)]

It teleported each player to a single spawn point instead of a random spawn point

You should remove your previous for loop and just keep the new one that I wrote adding the other lines you have inside the loop

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.