I've got a script to change the players team and spawn when they press a button but it doesnt work!

I’ve got a script to change the players team and respawn the player once they press the ‘play’ button because when they first spawn into the game they are set the ‘Spawning’ team and are spawned into a little box under the map, once they click the play button they are then put into the ‘In-Game’ team and are spawned into the map in the new team spawns around the map.

This all works ^
However once you die after that you don’t respawn on any of the ‘In-Game’ team spawn locations. Instead you spawn in the ‘Spawning’ team spawn location in that little box under the map. From my knowledge all the colors for the teams and spawn locations are set up correctly so I’m not sure why this is happening.

Code:

Local Script in my Gui

local button = script.Parent
local mainframe = script.Parent.Parent.Parent
local hud = game.StarterGui.Hud.Frame

local teams = game:GetService("Teams")
local player = game:GetService("Players").LocalPlayer
wait(5)
local sp1 = game.Workspace.SpawnLocation1.Position
local sp2 = game.Workspace.SpawnLocation2.Position
local sp3 = game.Workspace.SpawnLocation3.Position
local sp4 = game.Workspace.SpawnLocation4.Position
local sp5 = game.Workspace.SpawnLocation5.Position
local sp6 = game.Workspace.SpawnLocation6.Position
local spawnLocations = {sp1, sp2, sp3, sp4, sp5, sp6}

local characterLoader = game.ReplicatedStorage:WaitForChild("CharacterSpawner")

local function onButtonActivated()
	mainframe.Visible = false
	hud.Visible = true
	player.Team = teams["In-Game"]
	
	local randomSP = spawnLocations[math.random(1, #spawnLocations)] --Spawning didn't work so i had to manually teleport each player to their spawns but I don't see why I have to do this, they should just spawn on their spawn locations right?
	
	player.Character:MoveTo(randomSP)
	
end

button.Activated:Connect(onButtonActivated)

‘FirstSpawn’ Spawn Location located in the little box for when you first join the game which has Navy Blue which is the same color as ‘Spawning’ team
image

‘SpawnLocation 1-6’ All 6 of the other spawn locations associated with the Maroon color which is ‘In-Game’ team color
image

The whole idea of this script is so that the player can’t get killed whilst in the main menu.
Any Help is greatly appreciated as I am quite stumped and I imagine its something quite simple.

You’ll need to set and change the player’s team on the server using a RemoteEvent that fires after the player activates the play button in your game

Edit: @Sonnymacko Here’s how the LocalScript for the play button will need to be like:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local playButton = script.Parent

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local function onPlayButtonActivated()
	remoteEvent:FireServer()
end

playButton.Activated:Connect(onPlayButtonActivated)

And here’s how the server Script inside ServerScriptService will need to be like:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")

local remoteEvent = ReplicatedStorage.RemoteEvent

local inGameTeam = Teams["In-Game"]

local function onServerEvent(player)
	player.Team = inGameTeam

	player:LoadCharacter()
end

remoteEvent.OnServerEvent:Connect(onServerEvent)

You need to use a remote event to change the team of the player on the server side!

Thanks for the reply!. I’ve tried to do what i think is a RemoteEvent to change the players team and it still doesn’t work, It puts the player in the team however the player still spawns at the ‘Spawning’ team spawn instead of the ‘in-game’ spawn.

Heres my updated code:

Local Script

local button = script.Parent
local mainframe = script.Parent.Parent.Parent
local hud = game.StarterGui.Hud.Frame

local teams = game:GetService("Teams")
local player = game:GetService("Players").LocalPlayer
wait(5)
local sp1 = game.Workspace.SpawnLocation1.Position
local sp2 = game.Workspace.SpawnLocation2.Position
local sp3 = game.Workspace.SpawnLocation3.Position
local sp4 = game.Workspace.SpawnLocation4.Position
local sp5 = game.Workspace.SpawnLocation5.Position
local sp6 = game.Workspace.SpawnLocation6.Position
local spawnLocations = {sp1, sp2, sp3, sp4, sp5, sp6}
local randomSP = spawnLocations[math.random(1, #spawnLocations)]

local re = game.ReplicatedStorage.changeTeam

local function onButtonActivated()
	mainframe.Visible = false
	hud.Visible = true

	re:FireServer(player)
	print("Changing Players team from local script")	
	--player.Character:MoveTo(randomSP)
	
end

button.Activated:Connect(onButtonActivated)

Server Script

local re = game.ReplicatedStorage.changeTeam
local teams = game:GetService("Teams")

re.OnServerEvent:Connect(function(player)
	player.Team = teams["In-Game"]
end)

image
My ReplicatedStorage layout

EDIT: Nevermind it’s fixed!

Thanks heaps, I tried doing my own RemoteEvent and it still didn’t work. Appreciate the example it fixed it !

1 Like

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