Lives Counter doesn't display according to the Lives Value?

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.

In the Humanoid.Died function script you start out by defining the local player. How is this possible if it is a server script inside ServerScriptService?

If it is a local script that it will not work since only server scripts can be run inside ServerScriptService.

If that is a local script you would also not be able to change the NumValue via the server, which would explain why the server while the loop isn’t working.

2 Likes

The lives_amount variable just stores the number of lives; it doesn’t reference the Lives NumberValue

local lives_amount = game.Players.LocalPlayer.ValueFolder.Lives.Value

is pretty much the same as

local lives_amount = 3

So subtracting 1 from lives_amount only updates the variable, and does not update the Lives NumberValue. You should instead store a reference to the NumberValue so you can change it directly.

local lives_amount = game.Players.LocalPlayer.ValueFolder.Lives
lives_amount.Value = lives_amount.Value - 1 --updates the NumberValue
2 Likes

You forgot to set the value’s parent:

lives.Parent = playerVal

Don’t worry, alot of people made this mistake, kinds embarrassing but normal.

When I created the Instance of the Number Value, didn’t I already parented it to the Folder?
When I checked in the Client, or in other words, running the game. I found the NumberValue as a Child of the ValueFolder.

local lives = Instance.new("NumberValue", playerVal) 
1 Like

No he didn’t. He added the parent as the second parameter of the Instance.new(), which is a shortcut for setting the parent in a separate command.

Oh, I see. I was using a local script for the Text Label (The Counter).

1 Like

I’m sorry, but I can’t seem to understand what you mean by that statement.
Could you rephrase it?

I remember you from that other topic, I used the script that you provided.

After changing it, when I tested changing the Lives Value manually, the Counter changes to the correct Text. The problem with the Value is that when the player dies, the Value still doesn’t change.

Yes, sorry if I didn’t make it clear.

This is a Server Script since it is inside ServerScriptService. Correct?

Therefore you should not be calling the local player.

You cannot get the local player on the server.

1 Like

Ok, I understand it now. Thanks!

What I have changed after following @downcrusher69 's advice, is into this:

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
wait(1)
local lives_amount = player.ValueFolder.Lives

Is it still the same thing as calling the LocalPlayer in a ServerScript?
I’m not too familiar with Roblox Studio, so I’m kinda blur when having problems related to scripting.

The output still does not show anything.

You basically did but in a in efficient way.

Instead of using a while loop, why not use GetPropertyChangedSignal or Changed event?

Also, you can’t get local player from a server script, only by a local script. Also, use :GetService instead of game.Players, it is much better.

Also, you are still using local player which is completely wrong, and therefore the script won’t execute the other lines.

Change it to:

local player = game:GetService("Players").PlayerAdded:Wait()

I don’t see the difference between :GetService and game.(insert child here). But I can check that out myself.

I haven’t thought about using Changed event yet. I can try it out.
I assume it is this:

lives.Value.Changed:Connect(function()
	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)

Yes, just remove the .Value after lives since you are using Changed. Please correct your variables as well. Basically, Players is a service and to be on the safe side, you use :GetService. Although you don’t REALLY need to use it but it is good practice to use :GetService.

So, after all this. This is your Live Counter Gui script.

--// Variables and Services

local player = game:GetService("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:WaitForChild("ValueFolder"):FindFirstChild("Lives")

lives.Changed:Connect(function()
	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 here is your leaderstats script.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)

	local playerVal = Instance.new("Folder")
	playerVal.Name = "leaderstats"
    playerVal.Parent = plr

	local lives = Instance.new("NumberValue")
	lives.Name = "Lives"
	lives.Value = 3
    lives.Parent = playerVal        

end)

Make SURE you name playerVal to leaderstats or else it won’t show up!

1 Like

Thank you very much for being very patient in helping me!

But I am still wondering why this function won’t print out the amount of Lives the player has.
And yet, it also doesn’t subtract the Lives when the player dies.

local player = game:GetService("Players").PlayerAdded:Wait()
wait(1)
local lives_amount = 3

wait(1)
game.Players.PlayerAdded:Connect(function(p)
    p.CharacterAdded:Connect(function(character)
        character.Humanoid.Died:Connect(function()
            if lives_amount > 0 then
				wait()
                lives_amount = lives_amount - 1
				player:WaitForChild("leaderstats"):FindFirstChild("Lives").Value = lives_amount
				print("You have "..lives_amount.." lives left!")
            else
				wait(1)
                print("Restarting Game!")
                lives_amount = 3
				player:WaitForChild("leaderstats"):FindFirstChild("Lives").Value = lives_amount
            end
        end)
    end)
end)

if lives_amount > 0 then

Is the condition met and does it print Restarting game? If it does, it means your condition isn’t met and your script is fine.

Nothing gets printed out.
30chars

local player = game:GetService("Players").PlayerAdded:Wait()

Remove this line and try again.

Nothing happened, no errors nor prints in the output…