Hello again,
I am trying to program a Lives system where upon death, a script will subtract the lives by 1 each death and the Lives counter displaying the Lives Value will be subtracted as well.
Currently, the Lives Value is set to 3. After 3 lives are gone, a GUI will be activated having a Respawn button to regain 3 Lives again.
The problem is that when the humanoid Dies, the lives will not be subtracted and the Lives Counter will be the same. I have checked the Value in the Client just to confirm and it hasn’t changed upon death.
I’ve searched up on the Developer Forum and I have found similar topics to mines, unfortunately the Topics weren’t answered. I did manage to get the Idea of what to type in my scripts though.
The Set-Up for the Lives Value, located in ServerScriptsService.
The Lives Counter GUI only displays the Text of the Amount of Lives from here on.
local player = game:GetService("Players")
player.PlayerAdded:Connect(function(plr)
local playerVal = Instance.new("Folder", plr)
playerVal.Name = "ValueFolder"
local lives = Instance.new("NumberValue", playerVal)
lives.Name = "Lives"
lives.Value = 3
end)
This is the Script for the Lives Counter GUI
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local counter = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local lives = player.ValueFolder.Lives
while true do
wait()
if lives.Value == 3 then
script.Parent.Text = "Ă—3 Lives"
elseif lives.Value == 2 then
script.Parent.Text = "Ă—2 Lives"
elseif lives.Value == 1 then
script.Parent.Text = "Ă—1 Lives"
else
script.Parent.Text = "Ă—0 Lives"
end
end
And this is the Humanoid.Died function script. It is located in ServerScriptsService.
When testing, the script doesn’t print out the amount of Lives the player has left, nor does the Value change after the Player Dies.
local lives_amount = game.Players.LocalPlayer.ValueFolder.Lives.Value
wait(1)
game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
if lives_amount > 0 then
lives_amount = lives_amount - 1
print("You have "..lives_amount.." lives left!")
wait()
else
wait(1)
print("Restarting Game!")
lives_amount = 3
end
end)
end)
end)
To rephrase everything, I am trying to create a Lives Counter that displays the Player’s Lives left in the game. Except, the counter doesn’t update when the Player Dies.
I am trying to look for an explanation on why the While true do loop does not update the Lives Counter GUI.
