Lives and losing one if you get hit or if you touch a block

Hello Developers,

I am RealTimeCreator, I am working on a 2D game and need help figuring out how to make a lives system as a GUI, and by that I mean it tells you in the bottom corner how many lives you have left and if you lose them all you respawn.

image
image

Currently it is just a GUI that says how many lives you have.

If any of you know how to do this or could help me figure it out it would be greatly appreciated.

Cheers, RTC

5 Likes

Hello! To do so, you can simply add a “IntValue” into the player. Each time a player touch a part, you can remove them one life and use FireClient() to update their lifes on the GUIs. Once they reached 0 lifes, you can use :LoadCharacter() to respawn the player.

3 Likes

So how would this be transferred to a script?

What do you mean by that? :thinking:

2 Likes

How would I write this down as a script and where would the script go?

1 Like

U can make a script which detects when a player die,
U can use Humanoid.Died event

And u should add a int value into the player which decrease 1 every time u die. And set the text of the gui to “lives:”…Intvalue.

1 Like

Nevermind. I just written a script and using Remove Events isn’t needed.

Server script:

-- Settings

local Part = game.Workspace.Part -- Which part must subtract a life when touched.
local Lifes = 4 -- How many lifes players start with.
local Cooldown = false -- This cooldown will prevent the script to remove too many lives in a very short time.
local WaitTime = 2 -- The cooldown value.

-- First, you must add a IntValue into the player when they're joining the game.

game.Players.PlayerAdded:Connect(function(Player)
	if not Player:FindFirstChild("Lifes") then
		local PlayerLifes = Instance.new("IntValue", Player)
		PlayerLifes.Name = "PlayerLifes"
		PlayerLifes.Value = Lifes
	end
end)

-- If a player touch a part, remove them a life, or respawn them if they have none.

Part.Touched:Connect(function(Hit)
	if Cooldown == false then
		Cooldown = true
		local CheckHumanoid = Hit.Parent:FindFirstChild("Humanoid")
		if CheckHumanoid then
			local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
			local PlayerLifes = Player:FindFirstChild("PlayerLifes")
			if PlayerLifes.Value == 0 then
				Player:LoadCharacter()
				PlayerLifes.Value = Lifes
			else
				PlayerLifes.Value = PlayerLifes.Value - 1
				game.ReplicatedStorage.UpdatePlayerLifes:FireClient(Player)
			end
		end
		wait(WaitTime)
		Cooldown = false
	end
end)

This script will subtract one life each time a player touch a part!

Local script:

local Player = game.Players.LocalPlayer
local Lifes = Player:FindFirstChild("PlayerLifes").Value

while wait(0.25) do
	if Lifes then
		script.Parent.Text = "You have " .. Lifes .. " life(s) left!"
	end
end

This script will displays how many lifes a player has.

Here’s the result! Hope this can help you with your game.

2 Likes

Where do i put these scripts??

Put the first script into game.ServerScriptService, and the second script into “TextLabel”. :wink:

image

2 Likes

So I have put all the scripts inb the right place but it does not work when I touch the ground.

Pictures

image
image
image
image image
image
image

1 Like

Is the floor called “Part”, And is it inside workspace?

local MinusLifeBlock = game.Workspace.Part
1 Like

I called the floor MinusLifePart because wouldn’t it get mixed up with normal parts?

1 Like

Then make :

local MinusLifeBlock = game.Workspace.Part

Into:

local MinusLifeBlock = game.Workspace.MinusLifePart
2 Likes