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.
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)
@AlmightyYumzyFindFirstChildOfClass 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
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
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.
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