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

Making players spawn at different spawn plates in the beginning of the game?

This is a localscript that runs when the player joins. It places the player at a spawnlocation. Basically it's different spawn plates scattered and I wanted it so everyone is on a seperate plate.

game.Workspace:WaitForChild(game.Players.LocalPlayer.Name)
local plr = game.Players.LocalPlayer.Character
local getplr = game.Players.LocalPlayer
function place()
	if game.Workspace.SpawnTowers.T1.Taken.Value == false then
		game.Workspace.SpawnTowers.T1.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T1.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 1
		
	elseif game.Workspace.SpawnTowers.T2.Taken.Value == false then
		game.Workspace.SpawnTowers.T2.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T2.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 2
		
	elseif game.Workspace.SpawnTowers.T3.Taken.Value == false then
		game.Workspace.SpawnTowers.T3.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T3.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 3
		
	elseif game.Workspace.SpawnTowers.T4.Taken.Value == false then
		game.Workspace.SpawnTowers.T4.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T4.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 4
		
	elseif game.Workspace.SpawnTowers.T5.Taken.Value == false then
		game.Workspace.SpawnTowers.T5.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T5.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 5
		
	elseif game.Workspace.SpawnTowers.T6.Taken.Value == false then
		game.Workspace.SpawnTowers.T6.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T6.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 6
		
	elseif game.Workspace.SpawnTowers.T7.Taken.Value == false then
		game.Workspace.SpawnTowers.T7.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T7.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 7
	
	elseif game.Workspace.SpawnTowers.T8.Taken.Value == false then
		game.Workspace.SpawnTowers.T8.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T8.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 8
		
	elseif game.Workspace.SpawnTowers.T9.Taken.Value == false then
		game.Workspace.SpawnTowers.T9.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T9.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 9
		
	elseif game.Workspace.SpawnTowers.T10.Taken.Value == false then
		game.Workspace.SpawnTowers.T10.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T10.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 10
		
	elseif game.Workspace.SpawnTowers.T11.Taken.Value == false then
		game.Workspace.SpawnTowers.T11.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T11.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 11
		
	elseif game.Workspace.SpawnTowers.T12.Taken.Value == false then
		game.Workspace.SpawnTowers.T12.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T12.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 12
		
	elseif game.Workspace.SpawnTowers.T13.Taken.Value == false then
		game.Workspace.SpawnTowers.T13.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T13.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 13
		
	elseif game.Workspace.SpawnTowers.T14.Taken.Value == false then
		game.Workspace.SpawnTowers.T14.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T14.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 14
		
	elseif game.Workspace.SpawnTowers.T15.Taken.Value == false then
		game.Workspace.SpawnTowers.T15.Taken.Value = true
		plr:MoveTo(game.Workspace.SpawnTowers.T15.Part.Position)
		getplr.PlayerGui.CC.Main.bot.s1.Value.Value = 15
	
	else return

	end
	
end
place()

After a player is done creating their character this runs to mark the spawn plate the player was at, not taken so other's can spawn here. I've been having problems and people are spawning on the same plate. There are no errors.

game.Workspace:WaitForChild(game.Players.LocalPlayer.Name)
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:connect(function()
	if script.Parent.Value.Value == 1 then
		workspace.SpawnTowers.T1.Taken.Value = false
	elseif script.Parent.Value.Value == 2 then
		workspace.SpawnTowers.T2.Taken.Value = false
	elseif script.Parent.Value.Value == 3 then
		workspace.SpawnTowers.T3.Taken.Value = false
	elseif script.Parent.Value.Value == 4 then
		workspace.SpawnTowers.T4.Taken.Value = false
	elseif script.Parent.Value.Value == 5 then
		workspace.SpawnTowers.T5.Taken.Value = false
	elseif script.Parent.Value.Value == 6 then
		workspace.SpawnTowers.T6.Taken.Value = false
	elseif script.Parent.Value.Value == 7 then
		workspace.SpawnTowers.T7.Taken.Value = false
	elseif script.Parent.Value.Value == 8 then
		workspace.SpawnTowers.T8.Taken.Value = false
	elseif script.Parent.Value.Value == 9 then
		workspace.SpawnTowers.T9.Taken.Value = false
	elseif script.Parent.Value.Value == 10 then
		workspace.SpawnTowers.T10.Taken.Value = false
	elseif script.Parent.Value.Value == 11 then
		workspace.SpawnTowers.T11.Taken.Value = false
	elseif script.Parent.Value.Value == 12 then
		workspace.SpawnTowers.T12.Taken.Value = false
	elseif script.Parent.Value.Value == 13 then
		workspace.SpawnTowers.T13.Taken.Value = false
	elseif script.Parent.Value.Value == 14 then
		workspace.SpawnTowers.T14.Taken.Value = false
	elseif script.Parent.Value.Value == 15 then
		workspace.SpawnTowers.T15.Taken.Value = false	
	end
	local callplr = game.Players.LocalPlayer.Character.Humanoid
	callplr.WalkSpeed = 20
	callplr.JumpPower = 50
	wait()
	
	--check if plr gets tools
	if plr.PlayerGui.CC.Main.Values.speedster.Value == true then
		game.ReplicatedStorage.GUIs.ControlHUD:FireServer()
		game.ReplicatedStorage.Tools.TimeRemnant:FireServer()
	end
	if plr.PlayerGui.CC.Main.Values.cold.Value == true then
		game.ReplicatedStorage.Tools.ColdGun:FireServer()
	end
	if plr.PlayerGui.CC.Main.Values.heat.Value == true then
		game.ReplicatedStorage.Tools.HeatGun:FireServer()
	end
	if plr.PlayerGui.CC.Main.Values.punch.Value == true then
		game.ReplicatedStorage.Tools.Punch:FireServer()
	end
	if plr.PlayerGui.CC.Main.Values.vibe.Value == true then
		game.ReplicatedStorage.Tools.Vibe:FireServer()
		plr.PlayerGui.Coordinates.text.Visible = true
		plr.PlayerGui.CiscoGUI.dir.Visible = true
	end 
	if plr.PlayerGui.CC.Main.Values.mist.Value == true then
		game.ReplicatedStorage.GUIs.TheMist:FireServer()
	end
	if plr.PlayerGui.CC.Main.Values.boombox.Value == true then
		game.ReplicatedStorage.Tools.Boombox:FireServer()
	end
	if plr.PlayerGui.CC.Main.Values.fly.Value == true then
		game.ReplicatedStorage.Tools.Fly:FireServer()
	end
	if plr.PlayerGui.CC.Main.Values.drumsticks.Value == true then
		game.ReplicatedStorage.Tools.Drumsticks:FireServer()
	end
	
	plr.PlayerGui.skrt:Destroy()
	
	game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
	game.Workspace.CurrentCamera.CameraType = "Custom"
	
	script.Parent.Parent.Parent.Parent:Destroy()
	-- Enter a script to move the player to a certain location.
	local connor =(math.random(13))
	if connor == 1 then
		plr.Character:MoveTo(workspace.Spawns.S1.Position)
	elseif connor == 2 then
		plr.Character:MoveTo(workspace.Spawns.S2.Position)
	elseif connor == 3 then
		plr.Character:MoveTo(workspace.Spawns.S3.Position)
	elseif connor == 4 then
		plr.Character:MoveTo(workspace.Spawns.S4.Position)
	elseif connor == 5 then	
		plr.Character:MoveTo(workspace.Spawns.S5.Position)
	elseif connor == 6 then
		plr.Character:MoveTo(workspace.Spawns.S6.Position)	
	elseif connor == 7 then
		plr.Character:MoveTo(workspace.Spawns.S7.Position)
	elseif connor == 8 then
		plr.Character:MoveTo(workspace.Spawns.S8.Position)
	elseif connor == 9 then
		plr.Character:MoveTo(workspace.Spawns.S9.Position)
	elseif connor == 10 then
		plr.Character:MoveTo(workspace.Spawns.S10.Position)
	elseif connor == 11 then
		plr.Character:MoveTo(workspace.Spawns.S11.Position)
	elseif connor == 12 then
		plr.Character:MoveTo(workspace.Spawns.S12.Position)
	elseif connor == 13 then
		plr.Character:MoveTo(workspace.Spawns.S13.Position)
	end
	
	plr.PlayerGui.Teleport["Central City"].Visible = true
	plr.PlayerGui.Teleport.Flashpoint.Visible = true
	plr.PlayerGui.Teleport["STAR Labs"].Visible = true
	
end)

1 comment

0

This code is rather long and could be a lot more simple. Feel free to message me if you'd like a more personal explanation on why some things like indexing and metatables could help. As far as why it doesn't work; do you have an error? I know off the bat :WaitForChild() is not the safest method.

1 answer

✓ Accepted answer

I have coded something along the lines of what I would do. I have not yet tested this, as I am not at home.

local takenPlates = {} -- empty table containing used plates

-- What you would need to do is create enough plates to accomodate a full server, so if a server holds 20 maximim plateyers, create 20 plates.

local platesModel = workspace.spawnPlates

for _,plr in pairs(game.Players:GetPlayers()) do
	local plate = platesModel[math.random(1, #platesModel:GetChildren())] --Pick random plate
	for _,check in pairs(takenPlates) do --Loop through plates model
		if not check == plate then --Check that the selected plate is already taken
			--Code teleportation
			table.insert(takenPlates, plate) --Insert taken plate into table
		else
			repeat plate = platesModel[math.random(1, #platesModel:GetChildren())] wait() until plate ~= check -- Keep trying to find an available plate until one has been found
			--Code teleportation
		end
	end
end

1 comment

0

You would also need to empty the table between rounds if you plan on using this same script multiple times without resetting or re-inserting it.

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