Team change GUI Script not working

I’ve tried to make this script work and couldn’t understand where I had gone wrong. I’m trying to make this script change the player’s team and reset them. I’m not the best scripter so I had to resort to the forum.

local player = script.Parent.Parent

script.Parent.MouseButton1Click:Connect(function()
   player.Team = game.Teams:FindFirstChild("Police")
   player.character.head:destroy()
end)    

This script doesn’t reset the player or change the player’s team, can someone tell me how to fix it?

1 Like

I would suggest something like this:

script.Parent.Activated:Connect(function()
    player.Team = game.Teams.Police
    player.Character.Humanoid.Health = 0
end)

I just tried it, it doesn’t seem to change the team or reset the player.

Are you sure the the script.Parent is a button?

Yes.
Script

Oh I see, I would not suggest putting this into a script. Use a local script then fire a remote event!

Alright, I will try that. I’m not a scripter so I find this complicated.

I recommend you to use RemoteEvents and fire them when someone changes the team, your script looks like a 2012 code, try using modern Roblox physics instead.

Local Script: (Inside the button or anywhere)

-- Gets the player

local LocalPlayer = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()

-- I think the button has the script inside, you can change this.

local Button = script.Parent

-- As a example, Really Red is the current team color (You can change)

local SelectedTeamColor = BrickColor.new("Really Red")

-- You will need to use a RemoteEvent to fire server when someone changes the team.

local Event = game.ReplicatedStorage:WaitForChild("TeamEvent")



Button.MouseButton1Click:Connect(function()
  Event:FireServer(BrickColor.new(SelectedTeamColor) -- You can use BrickColor.new().
end)    

Script: (I recommend in ServerScriptService)

local Event = game.ReplicatedStorage:WaitForChild("TeamEvent")

Event.OnServerEvent:Connect(function(Player,ColorOfTeam)
Player.TeamColor = TeamColor
Player:LoadCharacter() -- You can use TakeDamage if you want to kill player!
end)

Hope this works! Have good luck with your game!

Thank you, I also have a little trouble with this script, like I said, I’m not a good scripter. I’m mainly a builder. Can you please tell me what is making this script not function?
Script

Did you create the remote event in replicated storage?

Yes. I have the remote event in replicated storage.

You are having an error on a the last function!

You forgot to add “)” on Event:FireServer(BrickColor.new(SelectedTeamColor)

It is

Event:FireServer(BrickColor.new(SelectedTeamColor)) And you made another brick color, remember the team color is the SelectedTeamColorValue.

Hey, I think this should be in the Scripting Support category.

You don’t have to keep using Parent to find the player, try this instead:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    player.Team = game.Teams:FindFirstChild("Police")
    player.Character.Head:destroy()
end)   

I am assuming you want to change someone’s team and respawn them so instead you can just use remote events. Fire the server from that local script code and then the server can instead change the team and kill the character in whatever way you want. I recommend just using :LoadCharacter() on the player instead of removing their head.

This is what it’ll look like (place remote event named TeamSwitchEvent in ReplicatedStorage):

Local script:

local remoteEvent = game.ReplicatedStorage.TeamSwitchEvent -- this is your remote event

script.Parent.MouseButton1Click:Connect(function()
    remoteEvent:FireServer()
end)

Server script:

local remoteEvent = game.ReplicatedStorage.TeamSwitchEvent -- same remote event
local policeTeam = game.Teams.Police

remoteEvent.OnServerEvent:Connect(function(player) -- player gets passed as a parameter
    player.Team = policeTeam  -- set their new team
    player:LoadCharacter() -- respawn the character
end)

The player could also spam the button to respawn themselves if they’re already police so you could use an if statement on the server code like this:

local remoteEvent = game.ReplicatedStorage.TeamSwitchEvent -- same remote event
local policeTeam = game.Teams.Police

remoteEvent.OnServerEvent:Connect(function(player) -- player gets passed as a parameter
    if player.Team ~= policeTeam then
        player.Team = policeTeam  -- set their new team
        player:LoadCharacter() -- respawn the character
    end
end)

Or, if you can understand guard clauses:

local remoteEvent = game.ReplicatedStorage.TeamSwitchEvent -- same remote event
local policeTeam = game.Teams.Police

remoteEvent.OnServerEvent:Connect(function(player) -- player gets passed as a parameter
    if player.Team == policeTeam then return end -- stop the function if the player is already in the police side.
    player.Team = policeTeam  -- set their new team
    player:LoadCharacter() -- respawn the character
end)

That is similar as my response…

I just provided examples & code to help him with it…

But its the same code of my example, you just changed variables.

I just want to say something, there is NO need to use remote events here, i have done an system like this and i used no remote events.

Oh I did not see it for some reason must have slipped past me, my reply works just as much don’t worry about it.