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.
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.
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.
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.