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
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.
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
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
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)
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)