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

Can somebody please explain why cash is not paying out until player dies on leaderboard?

Hello! I am trying to make a leaderboard for a game that computes kills, deaths & awards cash the longer a player is in the game.

It works great except the Cash does not begin paying out until the player dies.

I've moved it around and am still running into problems- Any suggestions?

Thanks.

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

	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent= plr 
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = stats 

	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = stats 

	local score = Instance.new("IntValue") 
	score.Name = "Cash" 
	score.Parent = stats
	score.Value = 1


	plr.CharacterAdded:Connect(function(char)
		
		local humanoid
		
		repeat 
			humanoid = char:FindFirstChild("Humanoid")
			wait()
		until humanoid
		
		humanoid.Died:Connect(function()

			deaths.Value = deaths.Value + 1
			
			local tag = humanoid:FindFirstChild("creator")
			
			if tag then

				local killer = tag.Value
				
				if killer then
				
					killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1

				end
			end
		while true do
		wait(5)
		score.Value = score.Value + 3
			end
		end)
	end)
end)

1 answer

✓ Accepted answer

If you notice your ends

			end
		end)
	end)
end)

The while loop is inside of the Died event listener!

You should move the loop inside of the PlayerAdded event listener.

The code is not indented well, and this could be why it is hard to see the issue.

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

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

    local kills = Instance.new("IntValue")
    kills.Name = "Kills"
    kills.Parent = stats 

    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Parent = stats 

    local cash = Instance.new("IntValue") -- # this is cash! not score
    cash.Name = "Cash" 
    cash.Parent = stats
    cash.Value = 1

    plr.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid") -- # Do not loop wait until something exists.

        humanoid.Died:Connect(function()
            deaths.Value = deaths.Value + 1
            local tag = humanoid:FindFirstChild("creator")

            if tag then
                local killer = tag.Value

                if killer then
                    killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
                end
            end
        end)
    end)

    while true do -- # in the PlayerAdded event listener
        wait(5)
        cash.Value = cash.Value + 3
    end
end)

2 comments

0

Don't forget to hit "Accept Answer" if this helped you.

0

Doh! I've been banging my head over this forever. THANK YOU!!!!!!!!!!!!!!!!

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