How can i data store a leaderstats value when it changes locally?

i have a script in the serverscriptservice that creates two values in a leaderstats,
bank which is the money stored in your bank
cash which is the money stored in your wallet

and heres the script that saves these 2 values and creates them

Script (server)
local datastoreservice = game:GetService("DataStoreService")

local MoneyDataStore = datastoreservice:GetDataStore("MoneyDataStore")

local players = game.Players



players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder")
	
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Cash = Instance.new("IntValue")
	
	Cash.Parent = leaderstats
	Cash.Name = "Cash"
	
	local Bank = Instance.new("IntValue")

	Bank.Parent = leaderstats
	Bank.Name = "Bank"
	

	local data
	
	local successfull, errormessage = pcall(function()
	
	data = MoneyDataStore:GetAsync(plr.UserId)
		
	end)	
		
	if successfull and data then
		
		Cash.Value = data.Cash
		Bank.Value = data.Bank
		
		print("Data Loaded Successfully")
		
	else
		
		Cash.Value = 100
		Bank.Value = 100
		
		warn(errormessage)
		
	end
	
end)

players.PlayerRemoving:Connect(function(plr)
	
	local successfull, errormessage = pcall(function()
		
		local data = {

			Bank = plr.leaderstats.Bank.Value;

			Cash = plr.leaderstats.Cash.Value;

		}
		
		MoneyDataStore:SetAsync(plr.UserId, data)

	end)	

	if successfull then
		
		print("Data Saved Successfully")

	else

		print("Encountered An Error While Saving Data")
	end
	
end)

and theres local scripts that changes these values,
the problem is that when the values are changed LOCALLY the server does not see the changes because they are changd locally , and other local scripts dont see the changes for some reason , i tried this and it didnt work

script (local)
local player = game.Players.LocalPlayer

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

	plr.leaderstats.Cash:GetPropertyChangedSignal("Value"):Connect(function() -- this is just for the cash value
	
		print(plr.leaderstats.Cash.Value)
	
	end)

end)

any help will be appreciated , i just want a script that sees if the cash value or the bank value has been changed locally and if it has then send it to the server via an event and then data store it,

i have the solution but its not general , its just that when a specific thing happens it sends to the server not when the value of the cash or bank changes it sends

2 Likes

You have to use RemoteEvents for it to replicate to the server, but remember that you should never trust the client when it comes to sending values over.

I would recommend for example if you want to add points when a player touches something, make a server script handle it.

1 Like

You shouldn’t trust the client to change the values because an exploiter could change the values to whatever they want, instead you should do everything on the server

1 Like

i know that but the problem is that i dont know how to do it lol ,

What are you trying to achieve? We can help give you steps for a solution if you tell us some more details.

Fire a remoteevent to the server instead of changing the value on the client and let the server do it

1 Like

i have a gui that sends money from the wallet to the bank , and from the bank to the wallet, thats why i used local scripts i want it so when the player presses a button the amount of money sends to the bank or the cash, i want to use server scripts but im dealing with gui

Make a remote event send how much money the player wants to send. On the server, check if the player has that much money, and then add it to the bank.

Read this

alight i will try that thanks for the help so far

thank you for the help i will read it and come back to you all thanks for the help so far

i did it thank you very much

local script

script.Parent.MouseButton1Click:Connect(function()

	local value = script.Parent.Parent.MoneySentToBank.ToBankValue.Value

	game.ReplicatedStorage.Money["CheckIFHasMoney(Cash)"]:FireServer(value)

end)

script

local event = game.ReplicatedStorage.Money["CheckIFHasMoney(Cash)"].OnServerEvent:Connect(function(player , value)
	
	if value > player.leaderstats.Bank.Value then
		
		else
		
		player.leaderstats.Cash.Value -= value

		player.leaderstats.Bank.Value += value

	end
	
end)

and thank you too very much i used remote events as you suggested

I think you meant to do player.leaderstats.Cash.Value, not player.leaderstats.Bank.Value.

Fixed code:

local event = nil
event = game.ReplicatedStorage.Money["CheckIFHasMoney(Cash)"].OnServerEvent:Connect(function(player, value)
	--//I'm assuming you're giving money from your wallet to the bank? If so, use this code
	if player.leaderstats.Cash.Value >= value then
		player.leaderstats.Cash.Value -= value
		player.leaderstats.Bank.Value += value
	end
end)

No problem though, have a good day!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.