GUI that shows a value from the leaderboard

Hi,
I have made a leaderboard with a script that every time that you kill someone it adds 20 to a value called cash and 1 to a value called kills But now I am trying to make a ScreenGUI that shows the value of the amount of cash you have.
This is my script for the leaderboard.

game.Players.PlayerAdded:connect(function(player)
	
	local leaderstats = Instance.new("Folder",player)
 	leaderstats.Name = "leaderstats"

 	local cash = Instance.new("IntValue",leaderstats)
 	cash.Name = "Cash"
	cash.Value = 0

	local kills = Instance.new("IntValue",leaderstats)
	kills.Name = "Kills"
	kills.Value = 0
	
 
	player.CharacterAdded:connect(function(character)
  		character:WaitForChild("Humanoid").Died:connect(function(killed)
	
   			local CreatorTag = character.Humanoid:FindFirstChild("creator")

   			if CreatorTag and CreatorTag.Value then
	
     			local stats = CreatorTag.Value:WaitForChild("leaderstats")

				stats["Cash"].Value = stats["Cash"].Value + 20
				stats["Kills"].Value = stats["Kills"].Value + 1			
   			end
		end)
	end)
end)

image
Could someone please answer this?
Thanks.

2 Likes

Make a script that has a while wait() do statement.
Then add a line that changes the text of the label to the value of the leaderstat.

Or you can do a Changed function.

You could user Value.Changed:

local Players = game:GetService("Players")
local Cash = Players.LocalPlayer.leaderstats.Cash

Cash.Changed:Connect(function()
text = Cash.Value
end)

Would you put this in a local script?

Since the GUI only shows the player yeah.

So I see your problem, your are using 2 of the same functions

game.Players.PlayerAdded:connect(function()

There is no need to use the same function twice where you could just be doing it in the same function like so:

 game.Players.PlayerAdded:Connect(function(player)
    	local character = player.Character or player.CharacterAdded:Wait();
    	
    	local leaderstats = Instance.new("Folder",player);
    	leaderstats.Name = "leaderstats";

    	local cash = Instance.new("IntValue",leaderstats);
    	cash.Name = "Cash";
    	cash.Value = 0;

    	local kills = Instance.new("IntValue",leaderstats);
    	kills.Name = "Kills";
    	kills.Value = 0;

    	character:WaitForChild("Humanoid").Died:Connect(function(killed)
    		
    		local CreatorTag = character.Humanoid:FindFirstChild("creator");

    		if CreatorTag and CreatorTag.Value then
    			
    			leaderstats.Cash.Value =  leaderstats.Cash.Value + 20;
    			leaderstats.Kills.Value = leaderstats.Kills.Value + 1;
    		end
    	end)
    end)

Now for the Cash display script you would need to use a local script as it will be different values for different players.

Example of Local Script:

 local player = game.Players.LocalPlayer
    local character = player.Character or player.CharacterAdded:Wait()

    local leaderstats = player:WaitForChild('leaderstats')

    local cash = leaderstats:WaitForChild('Cash')

    game.Players.PlayerAdded:Connect(function()
    	script.Parent.AttributeChanged = '$ '..cash.Value
    	
    	cash.Changed:Connect(function()
    		script.Parent.Text = '$ '..cash.Value
    	end)
    end)

This does not work.
I put the first script inside of a script inside of ServerScriptService and the second inside of the local script (which is inside of the Text Lable and the Text Lable is inside of the Cash Gui Which is in StarterGUI). Also when I tested instead of the person getting money for killing the other the person who died he gets the money and the kill counter.

Ive made changes to your server script that updates the values when someone gets a kill and earns the cash, and im sure this will work. Use the following code for your server script and replace everything that is currently inside the script:

game.Players.PlayerAdded:Connect(function(plr)
   
   local leaderstats = Instance.new("Folder", plr)
   leaderstats.Name = "leaderstats"
   
   local cash = Instance.new("IntValue", leaderstats)
   cash.Name = "Cash"
   cash.Value = 0
   
   local kills = Instance.new("IntValue", leaderstats)
   kills.Name = "Kills"
   kills.Value = 0
   
   plr.CharacterAdded:Connect(function(char)
      local hum = char:WaitForChild("Humanoid")
      
      hum.Died:Connect(function()
         local tag = hum:FindFirstChild("creator")
         
         if tag then
            local killer = tag.Value

            killer:FindFirstChild("leaderstats").Kills.Value += 1
            killer:FindFirstChild("leaderstats").Cash.Value += 20
         end
      end)
   end)
end)

Now for your local script (which should be inside of your cash text label), use this code:

local plr = game.Players.LocalPlayer
local cashValue = plr.leaderstats.Cash
local CashText = script.Parent

CashText.Text = cashValue.Value -- used just in case text resets

cashValue:GetPropertyChangedSignal("Value"):Connect(function() -- detects any changes in value
   CashText.Text = cashValue.Value -- Updates the Text 
end)
5 Likes

Ok it worked.
Thank you so much I have been trying to get a working script to do this for a long time and it really helps with my game.

3 Likes