Spawnlocations not working accordingly

I want to have custom team spawns that’ll spawn a player to a certain spawnlocation whenever on a certain team.

Every time I change teams, I spawn at only one spawn location.

I’ve tried manually changing my team via my player model within ‘Players’, I’ve also used my team change GUI which has the code listed below. I’ve tried looking for other posts, but couldn’t find any due to the DevForum not understanding the definition of ‘Relevant’.

I have AutoAssignable off and Neutral off but that didn’t change a thing.

Code
local HCTeam = Teams["High Command"]
local TATeam = Teams["Training Academy"]
local PersonnelTeam = Teams.Personnel
local CTeam = game.Teams.Civilian
local OpenerButton = script.Parent.OPENER
local MainFrame = script.Parent["IMAGE FRAME"]
local PButton = MainFrame.Personnel
local HCButton = MainFrame.HighC
local TAButton = MainFrame.TA
local LCE = game.ReplicatedStorage:WaitForChild("LoadChar")
local GroupID = 32637334
local Player = game.Players.LocalPlayer
local Rank = Player:GetRankInGroup(GroupID)

if Rank < 110 then
	HCButton.Visible = false
	elseif Rank >= 100 then
	HCButton.Visible = true
end
	
if Player:IsInGroup(GroupID) then
	PButton.Visible = true
	elseif not Player:IsInGroup(GroupID) then
	PButton.Visible = false
end
	
	if Rank < 100 then
		TAButton.Visible = false
		elseif Rank >= 100 then
		TAButton.Visible = true
	end
		
		
		HCButton.MouseButton1Click:Connect(function()
			Player.Team = HCTeam
			task.wait(0.56)
			LCE:FireServer()
			if Rank < 110 then
				Player.Team = Player.Team
			end
		end)
		TAButton.MouseButton1Click:Connect(function()
			Player.Team = TATeam
	task.wait(0.56)
	LCE:FireServer()
			if Rank < 100 then
				Player.Team = Player.Team
			end
		end)
		
		PButton.MouseButton1Click:Connect(function()
			Player.Team = PersonnelTeam
	task.wait(0.56)
	LCE:FireServer()
			if not Player:IsInGroup(GroupID) then
				Player.Team = Player.Team
			end
		end)
		
		MainFrame.Civ.MouseButton1Click:Connect(function()
			Player.Team = CTeam
			task.wait(0.56)
	LCE:FireServer()
		end)
		
		OpenerButton.MouseButton1Click:Connect(function()
			MainFrame.Visible = not MainFrame.Visible
		end)	```

(If I did the tags or category wrong, please let me know.)

THe category for this post is wrong. It should be scripting help!!

1 Like

Can u show me the full code, because it only shows a certain part!

That should be the entire code.

By the way, you can use the RespawnLocation to set the player team spawn. All you have to do is to set the RespawnLocation to a SpawnLocation.

Documentation: Player | Documentation - Roblox Creator Hub

I used RespawnLocation within my code but it still didn’t work, the code now looks like this:

local HCTeam = Teams["High Command"]
local TATeam = Teams["Training Academy"]
local PersonnelTeam = Teams.Personnel
local CTeam = game.Teams.Civilian
local OpenerButton = script.Parent.OPENER
local MainFrame = script.Parent["IMAGE FRAME"]
local PButton = MainFrame.Personnel
local HCButton = MainFrame.HighC
local TAButton = MainFrame.TA
local LCE = game.ReplicatedStorage:WaitForChild("LoadChar")
local GroupID = 32637334
local Player = game.Players.LocalPlayer
local Rank = Player:GetRankInGroup(GroupID)

if Rank < 110 then
	HCButton.Visible = false
	elseif Rank >= 100 then
	HCButton.Visible = true
end
	
if Player:IsInGroup(GroupID) then
	PButton.Visible = true
	elseif not Player:IsInGroup(GroupID) then
	PButton.Visible = false
end
	
	if Rank < 100 then
		TAButton.Visible = false
		elseif Rank >= 100 then
		TAButton.Visible = true
	end
		
		
		HCButton.MouseButton1Click:Connect(function()
	Player.RespawnLocation = game.Workspace:FindFirstChild("HC")
		task.wait(1)
		Player.Team = HCTeam
			task.wait(0.56)
			LCE:FireServer()
			if Rank < 110 then
				Player.Team = Player.Team
			end
		end)
		TAButton.MouseButton1Click:Connect(function()
	Player.RespawnLocation = workspace:FindFirstChild("TA")
			task.wait(1)
			Player.Team = TATeam
	task.wait(0.56)
	LCE:FireServer()
			if Rank < 100 then
				Player.Team = Player.Team
			end
		end)
		
		PButton.MouseButton1Click:Connect(function()
	Player.RespawnLocation = workspace:FindFirstChild("Personnel")
		task.wait(1)
		Player.Team = PersonnelTeam
	task.wait(0.56)
	LCE:FireServer()
			if not Player:IsInGroup(GroupID) then
				Player.Team = Player.Team
			end
		end)
		
		MainFrame.Civ.MouseButton1Click:Connect(function()
		Player.RespawnLocation = workspace:FindFirstChild("Civilian")
		task.wait(1)
			Player.Team = CTeam
			task.wait(0.56)
	LCE:FireServer()
		end)
		
		OpenerButton.MouseButton1Click:Connect(function()
			MainFrame.Visible = not MainFrame.Visible
		end)

Can you reformat ur code please? It is really hard to read with messy indents and spaces.

Also use this to format code:

```lua

```

1 Like
local Teams = game:GetService("Teams")
local HCTeam = Teams["High Command"]
local TATeam = Teams["Training Academy"]
local PersonnelTeam = Teams.Personnel
local CTeam = game.Teams.Civilian
local OpenerButton = script.Parent.OPENER
local MainFrame = script.Parent["IMAGE FRAME"]
local PButton = MainFrame.Personnel
local HCButton = MainFrame.HighC
local TAButton = MainFrame.TA
local LCE = game.ReplicatedStorage:WaitForChild("LoadChar")
local GroupID = 32637334
local Player = game.Players.LocalPlayer
local Rank = Player:GetRankInGroup(GroupID)

if Rank < 110 then
	HCButton.Visible = false
	elseif Rank >= 100 then
	HCButton.Visible = true
end
	
if Player:IsInGroup(GroupID) then
	PButton.Visible = true
	elseif not Player:IsInGroup(GroupID) then
	PButton.Visible = false
end
	
	if Rank < 100 then
		TAButton.Visible = false
		elseif Rank >= 100 then
		TAButton.Visible = true
	end
		
		
		HCButton.MouseButton1Click:Connect(function()
	Player.RespawnLocation = game.Workspace:FindFirstChild("HC")
		task.wait(1)
		Player.Team = HCTeam
			task.wait(0.56)
			LCE:FireServer()
			if Rank < 110 then
				Player.Team = Player.Team
			end
		end)
		TAButton.MouseButton1Click:Connect(function()
	Player.RespawnLocation = workspace:FindFirstChild("TA")
			task.wait(1)
			Player.Team = TATeam
	task.wait(0.56)
	LCE:FireServer()
			if Rank < 100 then
				Player.Team = Player.Team
			end
		end)
		
		PButton.MouseButton1Click:Connect(function()
	Player.RespawnLocation = workspace:FindFirstChild("Personnel")
		task.wait(1)
		Player.Team = PersonnelTeam
	task.wait(0.56)
	LCE:FireServer()
			if not Player:IsInGroup(GroupID) then
				Player.Team = Player.Team
			end
		end)
		
		MainFrame.Civ.MouseButton1Click:Connect(function()
		Player.RespawnLocation = workspace:FindFirstChild("Civilian")
		task.wait(1)
			Player.Team = CTeam
			task.wait(0.56)
	LCE:FireServer()
		end)
		
		OpenerButton.MouseButton1Click:Connect(function()
			MainFrame.Visible = not MainFrame.Visible
		end)

Use :WaitForChild() instead of :FindFirstChild()

Btw, try to read this article because I have see that there is something related about the team spawning location.

1 Like

I used :MoveTo to set the player’s position to the spawnlocation but everytime I click one of the TextButtons, it says "‘Position’ is not a valid member of ‘Players.Jerold09362’ when the returned values within the event are the player and a spawnlocation.

Event Code
local LC = game.ReplicatedStorage:WaitForChild("LoadChar")

LC.OnServerEvent:Connect(function(player,spawnl)
		local position = spawnl.Position
		player:LoadCharacter()
		wait(1)
	player.Character:MoveTo(Vector3.new(position.X, position.Y,position.Z))
end)
UI Code
local Teams = game:GetService("Teams")
local HCTeam = Teams["High Command"]
local TATeam = Teams["Training Academy"]
local PersonnelTeam = Teams.Personnel
local CTeam = game.Teams.Civilian
local OpenerButton = script.Parent.OPENER
local MainFrame = script.Parent["IMAGE FRAME"]
local PButton = MainFrame.Personnel
local HCButton = MainFrame.HighC
local TAButton = MainFrame.TA
local LCE = game.ReplicatedStorage:WaitForChild("LoadChar")
local GroupID = 32637334
local Player = game.Players.LocalPlayer
local Rank = Player:GetRankInGroup(GroupID)



if Rank < 110 then
	HCButton.Visible = false
	elseif Rank >= 100 then
	HCButton.Visible = true
end
	
if Player:IsInGroup(GroupID) then
	PButton.Visible = true
	elseif not Player:IsInGroup(GroupID) then
	PButton.Visible = false
end
	
	if Rank < 100 then
		TAButton.Visible = false
		elseif Rank >= 100 then
		TAButton.Visible = true
	end
		
		
		HCButton.MouseButton1Click:Connect(function()
		Player.Team = HCTeam
			task.wait(0.56)
			LCE:FireServer(Player,workspace:FindFirstChild("HC"))
			if Rank < 110 then
				Player.Team = Player.Team
			end
		end)
		TAButton.MouseButton1Click:Connect(function()
			Player.Team = TATeam
	task.wait(0.56)
	LCE:FireServer(Player,workspace:FindFirstChild("TA"))
			if Rank < 100 then
				Player.Team = Player.Team
			end
		end)
		
		PButton.MouseButton1Click:Connect(function()
		Player.Team = PersonnelTeam
	task.wait(0.56)
	LCE:FireServer(Player,workspace:FindFirstChild("Personnel"))
			if not Player:IsInGroup(GroupID) then
				Player.Team = Player.Team
			end
		end)
		
		MainFrame.Civ.MouseButton1Click:Connect(function()
			Player.Team = CTeam
			task.wait(0.56)
	LCE:FireServer(Player,workspace:FindFirstChild("Civilian"))
		end)
		
		OpenerButton.MouseButton1Click:Connect(function()
			MainFrame.Visible = not MainFrame.Visible
		end)

I did use both FindFirstChild and WaitForChild within the UI code.

Right, so the original post you referenced from a few years ago was a problem I had understanding replication. Looking at OP’s code and assuming it’s a LocalScript, they are likely having an issue understanding replication as well.

The easiest way to explain what’s going on is through Roblox’s article on the client-server runtime. The LocalScript, or client in this case, is making a change that the server doesn’t know about, and the server is still reading its own data about the player’s team which is different from what the client believes is their team. The server manages spawning the player for the most part so it reads off its own information, rather than reading off of the client’s information.

Your solution to this lies in Remote Events which would allow the client to request their team change to the server. This is its own can of worms though and I’d definitely recommend that you read up thoroughly on how this all works, as well as the security concerns it may bring.

Changing their team on the server and then respawning them should be good enough as the spawns sound like they’re set up correctly.

1 Like

With your help, and telling me to read about Remote Events I learned how to make the client communicate with the server by changing ‘(args)’ with the team, which was what I was doing wrong the entire time. Thank you to both of you. I will look into the security concerns that may follow with it.

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