How could I do this?

So I want a player to switch teams when they die.

my script is:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
game.Workspace.Part.Touched:Connect(function(touched)
	if touched.Parent:FindFirstChild("Humanoid") then
		local AnimPlay = touched.Parent.Humanoid:LoadAnimation(Animation)
		touched.Parent.HumanoidRootPart.Anchored = true
		AnimPlay:Play()
		AnimPlay.Stopped:wait() --Waits until it finished playing
		touched.Parent.HumanoidRootPart.Anchored = false
		player.TeamColor = BrickColor.new("Really red")
		if player.TeamColor == BrickColor.new("Really red") then
			script.Disabled = true
			if player.TeamColor == BrickColor.new("White") then
				script.Disabled = false
			end		
		end	
	end
end)

( this is a local script)

I just need to know how to make it so u go on a certain team if u die.

1 Like

Create a Server Script in ServerScriptService and paste the following code;

local team = 'putTeamNameHere' -- put your desired team's name here

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild('Humanoid').Died:Connect(function()
            player.Team = game.Teams[team]
        end)
    end)
end)

1 Like

I did just now @nLocaI!

I know, it’s supposed to work. but when I reset I’m still the “Infected” team. what I did was in my script, if I touch the block I go on the infected team. What I want is if I die, in any type of way… I go back to the “Humans” team. I shouldn’t stay the infected team if I die.

local team = 'Humans' 

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        wait(1)
        character:WaitForChild('Humanoid').Died:Connect(function()
            player.Team = game.Teams[team]
        end)
    end)
end)

This should work, try this

i tried that, it’s still not working for me.

Works for me, make sure it’s a normal Script and not a Local Script, and make sure it is somewhere it can run like Workspace or ServerScriptService. If not that then make sure your ‘humans’ team is named ‘Humans’

also, make sure the name of the team is EXACTLY the same

I have done ALL of that, nothing is working.

I don’t know what to tell you, this is down to you now, I mean i’ve provided you with sufficient working code that you needed so any problems are on your side.

there are prob something wrong with my script

Send your script here so I can check it

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
game.Workspace.Part.Touched:Connect(function(touched)
	if touched.Parent:FindFirstChild("Humanoid") then
		local AnimPlay = touched.Parent.Humanoid:LoadAnimation(Animation)
		touched.Parent.HumanoidRootPart.Anchored = true
		AnimPlay:Play()
		AnimPlay.Stopped:wait() --Waits until it finished playing
		touched.Parent.HumanoidRootPart.Anchored = false
		player.TeamColor = BrickColor.new("Really red")
		if player.TeamColor == BrickColor.new("Really red") then
			script.Disabled = true
			if player.TeamColor == BrickColor.new("White") then
				script.Disabled = false
			end		
		end	
	end
end)

this is the local script that might be making it not work… (StarterPlayer >> StarterPlayerScripts)

local team = 'Humans' 

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        wait(1)
        character:WaitForChild('Humanoid').Died:Connect(function()
            player.Team = game.Teams[team]
        end)
    end)
end)

the normal script in serverscriptservice

Do you get any errors, if so state them

nope, no errors at all. so idk.