Help with Team Select

Hello,

I am currently working on a game and I am in need of assistance with making the Team Selection GUI. So when the player selects the team they want to play as nothing happens. They are meant to be reset and spawn at the spawn point selected for that team.

The Script:

local player = game.Workspace:FindFirstChildOfClass("Player")
local hrp = player.HumanoidRootPart
local overseer = game.Teams.Overseer

script.Parent.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.Team = overseer
	hrp:Destroy() -- So the player can spawn at the correct spawnpoint
	script.Parent.Parent.Visible = false
end)

The output gives no errors. I have tried this in both a Local Script and a Regular Script.

Thank You. :slight_smile:

2 Likes

Hmmm, findfirstchildofclass doesent seem right to me lol but it proberly is try findfirstchild…

1 Like
local player = game.Workspace:FindFirstChild("Player")
local hrp = player.HumanoidRootPart
local overseer = game.Teams.Overseer

script.Parent.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.Team = overseer
	hrp:Destroy() -- So the player can spawn at the correct spawnpoint
	script.Parent.Parent.Visible = false
end)
1 Like

Try that to see if it works reply if it doesent.

1 Like

@AlmightyYumzy FindFirstChildOfClass is a method to find a child of a specific class without respecting inheritance

@pinchpotmonster Use LocalPlayer to get the player instnace and get the character from there

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
local overseer = game.Teams.Overseer

local button = script.Parent

button.MouseButton1Click:Connect(function()
	player.Team = overseer
	hrp:Destroy() -- Or you can just use player:LoadCharacter()
	button.Parent.Visible = false
end)

This will probably not work as the Team property is not replicated to the server, you will need a RemoteEvent for this I believe

1 Like

I suck at scriptng lol xd atleast I tried.

1 Like

It’s fine! You can’t be good at scripting immediately, it takes time to get better! But if you’re uncertain if a method exists, look it up!

Ahh is that how you sometimes get scripts to work is you search it up and learn?

Would this be used in a Local Script or just a Regular One?

Also how would I implement that, as I do not know how to use events properly?

Thank You

A localscript, and I mostly stated that since the server will still see that you are not in that team cause Team is non-replicated, meaning changes to the property in the localscript will not show up on the Server. A simple way to do that would be to make a RemoteEvent in ReplicatedStorage and a script in ServerScriptService with this code

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

repStore.RemoteEvent.OnServerEvent:Connect(function(player,teamname)
	local team = teams[teamname]
	if not team then return end
	player.Team = team
	player:LoadCharacter()
end)

I added a basic check so if the team with that name given doesn’t exist, it’ll stop so it doesn’t error when trying to change your team.

And then in a localscript i nthe button, you just fire to the server

local repStore = game:GetService("ReplicatedStorage")
local event = repStore.RemoteEvent

local button = script.Parent

button.MouseButton1Click:Connect(function()
	event:FireServer("Overseer")
	button.Parent.Visible = false
end)

Simplest way you can do it, make sure the name given is a valid team name.

Oh and to explain why I didn’t pass the player with the team name, it’s passed in automatically.

This is the basics of how you can do it, you will need to add some additional checks to prevent exploiters from changing team mid game

1 Like

I tried your scripts and only got 1 error.

Connect is not a valid member of RemoteEvent "ReplicatedStorage.RemoteEvent" 

This is in the script inside of ServerScrptService

Oh my bad, change

repStore.RemoteEvent:Connect(function(player,teamname)

To

repStore.RemoteEvent.OnServerEvent:Connect(function(player,teamname)
1 Like

Thank you very much,

Just one more question though. How do I make it so that the UI doesn’t re appear after the player has been Loaded again?
I tried Destroying the UI but it still re appeared.

1 Like

You can simply set the ResetOnSpawn property of the ScreenGui the Button is in to False, that way the Gui will not replicate to your PlayerGui on Respawn

1 Like

Ah, Alright. Thank you again for the help. :slight_smile:

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like

I’ve been dealing with a similar problem with pinchpotmonster, this helped me greatly! Thank you!

1 Like