How can I add or remove value to a leaderstat from another script?

I’m just trying to make a purchase system where when you purchase the in-game product it increases one of the leaderstats by 1 and decreases the other by 5 (separate leaderstats)

I don’t know how I am able to fetch the leaderstats from the player, I have tried doing

leaderstat1 = game.players.localplayer.leaderstats.Pounds
leaderstat2 = game.players.localplayer.leaderstats.Tickets

Here is my code:

local Part = script.Parent.Parent
local ProximityPrompt = script.Parent
local ls = game.Players.LocalPlayer.leaderstats.Pounds


ProximityPrompt.Triggered:connect(function(Player)
	-- Add value to one of the players leaderstats
	-- Remove value from a different leaderstat
	end)

If something doesn’t make sense, please let me know.
Thank you!

3 Likes

Where is your code that is creating the leaderstats?

image

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("POUNDID:36729346DDX")
local ds2 = dsService:GetDataStore("MacopolyID:36729346DDX")
local ran = math.random(1,5)
 
game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local currency = Instance.new("IntValue", folder)
    currency.Name = "Pounds"
	currency.Value = ds:GetAsync(plr.UserId) or 0
    currency.Changed:Connect(function()
		ds:SetAsync(plr.UserId, currency.Value)
		
	end)
	local Mac = Instance.new("IntValue", folder)
	Mac.Name = "Macopoly Tickets"
	Mac.Value = ds2:GetAsync(plr.UserId) or 0
	Mac.Changed:Connect(function()
		ds2:SetAsync(plr.UserId, Mac.Value)
		end)
	while true do
		currency.Value = currency.Value + ran
		wait(60)
	end
end)
 
game.Players.PlayerRemoving:Connect(function(plr)
    ds:SetAsync(plr.UserId, plr.leaderstats.Money.Value)
end)

The creation of the leaderstats is never completing because of the while true do loop

maybe try

spawn(function()
	while player and player.Parent do
		currency.Value = currency.Value + math.random(1,5)
		wait(60)
	end
end)
1 Like

ok I made some edits so its not a forever loop, it should exit when the player leaves the game,
also your random was not in the loop meaning it would have the same value for the added money every minute

1 Like

It works great thank you, but it doesn’t really answer my initial question.
In a separate script I am wanting to be able to change the leaderstat values

But my issue is I don’t know how I can actually find the players leaderstat to alter them.

Thank you for pointing out that issue however.

Well in another script, all you do is get the player who’s stats you want to change
and do

currency = player:WaitForChild(“leaderstats”):WaitForChild(“Pounds”)
currency.Value = currency.Value + 1

like that

1 Like

I don’t really know what I’m doing at this point,
I’m not sure what I’m doing wrong.

local Part = script.Parent.Parent
local ProximityPrompt = script.Parent
local Player = game.Players.LocalPlayer
currency = Player:WaitForChild("leaderstats"):WaitForChild("Pounds")
currency2 = Player:WaitForChild("leaderstats"):WaitForChild("Macopoly Tickets")



ProximityPrompt.Triggered:connect(function(Player)
	currency.Value = currency.Value - 5
	currency.Value = currency.Value + 1
end)

You’re editing the values on “localplayer” they will not replicate.
Use this instead.

local Part = script.Parent.Parent
local ProximityPrompt = script.Parent


ProximityPrompt.Triggered:connect(function(Player)
        currency = Player:WaitForChild("leaderstats"):WaitForChild("Pounds")
         currency2 = Player:WaitForChild("leaderstats"):WaitForChild("Macopoly Tickets")
          currency.Value = currency.Value -= 5 -- this will change pounds value
	currency2.Value = currency2.Value += 1 -- this will change tickets value
end)
1 Like

This still isn’t working unfortunately,

I get no errors, just warnings telling me to change the “currency” variable to local (which I have tried)

And “-=” just gave me an error so I changed it to “-”


	local Part = script.Parent.Parent
	local ProximityPrompt = script.Parent


	ProximityPrompt.Triggered:connect(function(Player)
		currency = Player:WaitForChild("leaderstats"):WaitForChild("Pounds")
	 currency2 = Player:WaitForChild("leaderstats"):WaitForChild("Macopoly Tickets")
		currency.Value = currency.Value - 5 -- this will change pounds value
		currency2.Value = currency2.Value + 1 -- this will change tickets value
	end)

What error did it give exactly? Because that script would work regardless.

local Part = script.Parent.Parent
local ProximityPrompt = script.Parent


ProximityPrompt.Triggered:connect(function(Player)
		local currency = Player:WaitForChild("leaderstats"):WaitForChild("Pounds")
	    local currency2 = Player:WaitForChild("leaderstats"):WaitForChild("Macopoly Tickets")
		currency.Value  -= 5 -- this will change pounds value
		currency2.Value += 1 -- this will change tickets value
	end)
2 Likes

Make that localscript a normal script instead. Localscripts DO NOT run in workspace.

2 Likes