Help with a three life team system

Hello!
Thank you for taking your time to read this:

I’m not really a scripter but I want to create this system where when a player joins a team, they will have three lives. After wasting them, the player resets back to another team. I’ve tried lots of scripts but they don’t work. Could any of you help me please? It’ll be greatly appreciated.

1 Like

Hopefully this will get you on the right track:

local teamLives = 3

-- Fire this whenever a team member dies (below)
teamLives = teamLives - 1

Hope this puts you on the right track!

1 Like

Thank you for your response but if you could provide a full script, it would be appreciated. As I said before, I’m not really a scripter. So it would be great to learn some more about scripting.

1 Like
local livescount = 3

local humanoid = script.Parent.Humanoid

humanoid.Died:Connect(function()

livescount = livescount - 1

end)


Mine is a basic script that I’ve been using, But @DR_TRAY11111 probably made a better one

2 Likes
local teamMembers = {member1,member2} -- table is an example
local lives = 3

for i, member in pairs(teamMembers) do
    member.Character.Humanoid.Died:Connect(function(died)
        lives = lives - 1
    end)
end
2 Likes

Thank you very much. I have a question, would this work with everyone in the team?

I cant really script very well but like @DR_TRAY11111 said
local teamLives = 3

– Fire this whenever a team member dies (below)
teamLives = teamLives - 1

1 Like

Hey there! This isn’t too hard to make, I think the best way to approach this by making an intvalue setting that intvalue equal to 3, and then every time the player died, negate one of that value, with it also checking if the value is equal to 0, change the player’s team.

You need to make a script into serverscriptservice, all the code will be written in a script in serverscriptservice.

First of all, let’s make the value, I’m not sure when you want the player to have this, but let’s assume you want the system whenever a player joins, so let’s create a simple player added function like this:

game.Players.PlayerAdded:Connect(function(player)
     
end)

This function will fire every time a player has joined the game, let’s now create the value.

game.Players.PlayerAdded:Connect(function(player)
     local lives = Instance.new("IntValue") -- Creating the value
     lives.Name = "Lives" -- Setting a name for the value
     lives.Parent = player -- Parenting the intvalue to the player
end)

Great, we now made the value, let’s now set it to 3 using this line of code, I think it’s pretty obvious how I do it.

lives.Value = 3

Alright, now we made our value, let’s now check when a player has died, we can do this using the humanoid.Died function, a simple build-in function that checks when a player has died. We do have to make sure the player’s character has already spawned in though, we can do this using the character added function:

game.Players.PlayerAdded:Connect(function(player)

     local lives = Instance.new("IntValue") -- Creating the value
     lives.Name = "Lives" -- Setting a name for the value
     lives.Value = 3
     lives.Parent = player -- Parenting the intvalue to the player

     player.CharacterAdded:Connect(function(character) -- Whenever the player's character has loaded in, this event will fire

     end)

end)

Now we can actually use the humanoid.Died event, we will also make use of the function WaitForChild, this function will wait for the humanoid, so it doesn’t get loaded too quickly, and to prevent any errors.

local humanoid = character:WaitForChild("Humanoid") -- Using the waitforchild methode

humanoid.Died:Connect(function() -- When the player has died, this function will run

end)

Alright, now we can just remove 1 live from the player by saying live.Value -= 1

local humanoid = character:WaitForChild("Humanoid") -- Using the waitforchild methode

humanoid.Died:Connect(function() -- When the player has died, this function will run
     lives.Value -= 1 -- Decrements one life of the player
end)

Almost there! We just have to make sure the player actually switches team, we can do it by using an if statement, we’ll say: if the player’s deaths are equal to 0, switch the player’s team.

local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()

     lives.Value -= 1

     -- Make sure to put this AFTER we have removed the one live from the player, so it doesn't check it too early
     if lives.Value == 0 then -- When using an if statement to check something, you use == instead of =, == is being used to check something and = is used to set something to something.
           
     end

end)

Alright, now we would have to change the player’s team, we would have to do this by using teamcolors, teams use color’s for this, as you can see, every team should have a different color, so let’s assume we want to switch to the police team, just set the team color to white. You can do that like this:

humanoid.Died:Connect(function()

     lives.Value -= 1

     -- Make sure to put this AFTER we have removed the one live from the player, so it doesn't check it too early
     if lives.Value == 0 then -- When using an if statement to check something, you use == instead of =, == is being used to check something and = is used to set something to something.
          player.TeamColor = BrickColor.new("White") -- We have to use brickcolor, since that's what teams use.
     end

end)

My team for instance
image

Make sure to change the brick color to your team color, also make sure you spelled your team color correctly, if you didn’t it won’t work.

Alright, this should actually be it, now, I am not sure if the player just get’s their life reset, if so, make sure to set the lives to 3 again.

     lives.Value -= 1

     -- Make sure to put this AFTER we have removed the one live from the player, so it doesn't check it too early
     if lives.Value == 0 then -- When using an if statement to check something, you use == instead of =, == is being used to check something and = is used to set something to something.
          player.TeamColor = BrickColor.new("White") -- We have to use brickcolor, since that's what teams use.
          lives.Value = 3
     end

If that’s not the case, please let me know, so I can help you with it.

Now, your script should look something like this:

game.Players.PlayerAdded:Connect(function(player)

	local lives = Instance.new("IntValue")
	lives.Name = "Lives"
	lives.Value = 3
	lives.Parent = player

	player.CharacterAdded:Connect(function(character)
		
		local humanoid = character:WaitForChild("Humanoid")

		humanoid.Died:Connect(function()

			lives.Value -= 1

			if lives.Value == 0 then
				player.TeamColor = BrickColor.new("White")
			end

		end)
		
	  end)

    end)

I didn’t really have time to test this yet or you want anything changed, so if it doesn’t work, please let me know.

I hope this helped you and also helped you gain more understanding of lua.

1 Like

Thank you very much for this. I will try and understand the script and test it out to see if it works. Again, thank you.

1 Like

I have a question. If I changed game.Player.PlayerAdded to this:
game.Team.<teamnamehere>.PlayerAdded

Would it change it to when the player joins a specific team?

Oh, I actually didn’t knew this, but yes, this is possible, this would break the script though. You would have to recode some parts to make that working.

1 Like

no, use childadded.

game.Team.TeamHere.ChildAdded:Connect(function(Child) 
       if Child.Character then -- Checking for the child's character
            if Child.Character:FindFirstChild("Humanoid") then -- Looking for a humanoid.
                 print("A player has been added to a team!")
          end
     end
end)

This script is untested, hopefully it should work though.

1 Like

This is possible.

Your code wouldn’t work, since you check when you add a whole new team, not specifically for that player.

1 Like

?, Im don’t understand…

Its doing:

game.Teams.TeamHere.ChildAdded:Connect(function() -- Checks when a child is added for a team.

Yeah, your checking if a whole new team has been added. Look, you go to the service teams, so this:

image

Your not checking if a team for a specific player has been added.

1 Like

So when a player joins a team they don’t actually join it??

Sorry if im not getting it but once again im doing:

game.Teams.TeamHere.ChildAdded:Connect(function()

The code would check when a player joined a team which is TeamHere?

1 Like

Your script works! Only thing that didn’t make it work is that you did player.TeamColor = "White" when it should’ve been player.TeamColor = BrickColor.new("White")

Now I just need to tailor it to make it when the player joins a team. Thank you very very much!
:smiley:

Your welcome! I indeed didn’t see that, good for pointing it out!

1 Like

Thank you. If you could edit that part correctly, I could add it as the solution.

Look, your using childadded right? Childadded fires when a new component has been added to something, so your checking if in the team you are, a new component has been added, and there are ni new components added to the team in the service teams.

1 Like