How to make my leaderstat follow the number on my counter?

Sorry for explaining bad but, i’ve got a counter for a button which when you click, it gives you a point on the counter, and i would want the points to go on the leaderstat. I don’t know scripting much. So can you help me? The name of the leaderstat is “Clicks”.

Scripts:

Leaderstat:

function playerentered(newPlayer)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local Money = Instance.new("IntValue")
	Money.Name = "Clicks"	
	Money.Value = 0

	Money.Parent = stats
	stats.Parent = newPlayer
end
game.Players.ChildAdded:connect(playerentered)

Script for button:

local buttonstart = script.Parent.ImageButton

local function onButtonActivated ()

local currentvalue = script.Parent.Parent.ScreenGui.Frame.counter.Text

script.Parent.Parent.ScreenGui.Frame.counter.Text = currentvalue + 1

end

buttonstart.Activated:Connect(onButtonActivated)

What you’re gonna wanna do is every time you click the button, fire a remote event to the server through a remote event. In your leaderstats script, add some code that adds the button value by 1 every time the remote event is fired.

1 Like

use something like this in the textlabel gui in a local script

while true do
   wait()
   script.Parent.Text = "Clicks: "..game.Players.LocalPlayer.leaderstats.Clicks.Value
end

this should copy the value from the leaderstats to the text label gui

No no, i have no value in the leaderstat. I have value on the textlabel and i want the value on the textlabel to be the same as the leaderstat.

then use the script.Parent.Text to print the stats on the text label

This is indeed the solution, and here is how it can be achieved:

Button LocalScript:

local buttonstart = script.Parent.ImageButton
local remote = game.ReplicatedStorage.ChangeValue -- Make a new RemoteEvent and change this value so it leads to it.

function onButtonActivated()
    local currentvalue = script.Parent.Parent.ScreenGui.Frame.counter.Text
    script.Parent.Parent.ScreenGui.Frame.counter.Text = currentvalue + 1
    remote:FireServer() -- Request leaderstats update from the server.
end

buttonstart.Activated:Connect(onButtonActivated)

Add to any ServerScript:

local remote = game.ReplicatedStorage.ChangeValue -- Value should be the same you put in the button LocalScript.

remote.OnServerEvent:Connect(function(plr) -- Update requested.
    plr.leaderstats.Clicks.Value = plr.leaderstats.Clicks.Value + 1 -- Update value.
end)
4 Likes