Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Unable to get lives to change when humanoid dies. Any Idea why?

I have been working on the script for a little while and I can get the lives to change but it changes the second the game starts and keeps counting down instead of only by 1 when they die. Any ideas guys. Here is the script for further inspection.

game.Players.PlayerAdded:connect(function(plr)
	local Stats = Instance.new('IntValue', plr)
	Stats.Name = 'leaderstats'
	
	local Lives = Instance.new('IntValue', Stats)
	Lives.Name = 'Lives'
	Lives.Value = 25
	
	local Stage = Instance.new('IntValue', Stats)
	Stage.Name = 'Stage'
	Stage.Value = 1
	
	local Timer = Instance.new('IntValue', Stats)
	Timer.Name = 'Time'
	Timer.Value = 0
	while true do
		wait(1)
		Timer.Value = Timer.Value +1

	if game.Players.LocalPlayer.Character.Humanoid.died 
		then do
		Lives.Value = Lives.Value -1
	end end end end)

Any help would be appreciated. I can't seem to understand why it's not working. I was referencing the wiki but to no luck.

3 answers

✓ Accepted answer

I am at school, not at home so I will not be able to test, sadly.

game.Players.PlayerAdded:Connect(function(plr)
    local Stats = Instance.new('Folder') --I rather use a folder. Make more sense.
    Stats.Name = 'leaderstats' --Name it to 'leaderstats' if you want ROBLOX's playerlist to show up showing each player's stats.
    Stats.Parent = plr

    local Lives = Instance.new('IntValue')
    Lives.Name = 'Lives'
    Lives.Value = 25
    Lives.Parent = Stats

    local Stage = Instance.new('IntValue')
    Stage.Name = 'Stage'
    Stage.Value = 1
    Stage.Parent = Stats

    local Timer = Instance.new('IntValue')
    Timer.Name = 'Time'
    Timer.Value = 0
    Timer.Parent = Stats
		
    plr.CharacterAdded:Connect(function(char)
  		char.Humanoid.Died:Connect(function() 
       		Lives.Value = Lives.Value -1
        end)
    end)
    
    while true do
        wait(1)
        Timer.Value = Timer.Value +1
    end
end)

You're formatting this script really weird...

First off, you have too many "ends". There should only be three, not four.

It's because Humanoid.Died is an event, not a property.

game.Players.PlayerAdded:connect(function(plr)
    local Stats = Instance.new('IntValue', plr)
    Stats.Name = 'leaderstats'

    local Lives = Instance.new('IntValue', Stats)
    Lives.Name = 'Lives'
    Lives.Value = 25

    local Stage = Instance.new('IntValue', Stats)
    Stage.Name = 'Stage'
    Stage.Value = 1

    local Timer = Instance.new('IntValue', Stats)
    Timer.Name = 'Time'
    Timer.Value = 0
    while true do
        wait(1)
        Timer.Value = Timer.Value +1
	game.Players.LocalPlayer.Character.Humanoid.Died:Connect(function(plr)
		Lives.Value = Lives.Value -1
	end)
    end 
end)

FYI: in if statement is formatted

if x == x then
	--code
end

You don't need the do after the then.

1 comment

0

Thank you for the explanation it really helps to understand now!

Died is a event signal like PlayerAdded that fires when (as the name implies) the Player's Character dies. You'd have to :Connect to it like you did with PlayerAdded:

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

-- Never use the 2nd argument for Instance.new(); it's bad performance-wise
    local Stats = Instance.new('IntValue')
    Stats.Name = 'leaderstats'
	Stats.Parent = plr

    local Lives = Instance.new('IntValue')
    Lives.Name = 'Lives'
    Lives.Value = 25
	Lives.Parent = Stats

    local Stage = Instance.new('IntValue')
    Stage.Name = 'Stage'
    Stage.Value = 1
	Stage.Parent = Stats

    local Timer = Instance.new('IntValue')
    Timer.Name = 'Time'
    Timer.Value = 0
	Timer.Parent = Stats

    while true do
        wait(1)
        Timer.Value = Timer.Value +1

-- Died has to be exactly spelt Died, because it's a special ROBLOX thing
    game.Players.LocalPlayer.Character.Humanoid.Died:Connect(function() 
        Lives.Value = Lives.Value -1
	end)

	end
end)

2 comments

0

Thank you that really helped me and helped me remove unnecessary code. Just one more question which is confusing me. I fixed my code to match but for some reason when I die now instead of just removing 1 life it removes 10 or more. Any idea why that would be?

0

It's fixed thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.