Why does the currency return to its original values after spending it, and it is not saved?

Hello everyone!

Why does the currency return to its original values ​​after spending it, and it is not saved.

This is footages proofing my word’s:


This is script of buy button:

local tws = game:GetService("TweenService")
local i = TweenInfo.new(0.3,Enum.EasingStyle.Sine,Enum.EasingDirection.In)
local plr = game.Players.LocalPlayer

script.Parent.MouseEnter:Connect(function()
	tws:Create(script.Parent,i,{Rotation = 10}):Play()
	game.SoundService.SFX["Enter & Back"]:Play()
end)
script.Parent.MouseLeave:Connect(function()
	tws:Create(script.Parent,i,{Rotation = 0}):Play()
end)
script.Parent.MouseButton1Click:Connect(function()
	game.SoundService.SFX["Enter & Back"]:Play()
    if plr.leaderstats["Fool's"].Value >= 100 or plr.leaderstats["Fool's"].Value == 100 then
		plr.leaderstats.Multiplier.Value = plr.leaderstats.Multiplier.Value +1
		plr.leaderstats["Fool's"].Value = plr.leaderstats["Fool's"].Value -100
	end
end)

Script of leaderstats:

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	local coins1 = Instance.new("NumberValue",leaderstats)
	coins1.Name = "Fool's"
	local coins2 = Instance.new("NumberValue",leaderstats)
	coins2.Name = "Multiplier"
end)

Script of datastore:

local data = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrid = "id_"..plr.UserId
	local save1 = plr.leaderstats["Fool's"]
	local save2 = plr.leaderstats.Multiplier
	local GetSaved = data:GetAsync(plrid)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else
		local nfs = {save1.Value,save2.Value}
		data:SetAsync(plrid,nfs)
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	data:SetAsync("id_"..plr.UserId,{plr.leaderstats["Fool's"].Value,plr.leaderstats.Multiplier.Value})
end)

This is short topic and sorry for my grammar issues, my english is bad.

2 Likes

Change the values from a server script, not from a client script

1 Like

Problem: You can’t change the value from your local client, but rather from the server, using a serverscript

The “Buy Button” script should fire a remote event, which then makes the player subtract currency.

1 - add a remote event in ReplicatedStorage name it “BuyItem”
2 -

script.Parent.MouseButton1Click:Connect(function()
   game.SoundService.SFX["Enter & Back"]:Play()
   if plr.leaderstats["Fool's"].Value >= 100 or plr.leaderstats["Fool's"].Value == 100 then
   	game:GetService("ReplicatedStorage").BuyItem:FireServer(plr.leaderstats.Multiplier, plr.leaderstats["Fool's"])
   end
end)

3 - Make a serverscript, preferably in serverscriptservice, to get the communication signal

game:GetService("ReplicatedStorage").BuyItem.OnServerEvent:Connect(function(player,Multiplier, Fools)

Multiplier.Value = plr.leaderstats.Multiplier.Value +1
FoolsValue = plr.leaderstats["Fool's"].Value -100

end)

Hope this helps!

This script make this:

Yeah because here you add multiplier. if youd subtract, youd do -1

I wanted the player buying value and not receive it.