Sorry if the title is vague. My issue is that I have a script that gives a player cash every x amount of seconds. It adds it, but it adds the wrong number? When I check the amount in leaderstats and in the folder its in it displays the correct amount, but the script starts back from 1000.
I have a data save script, wondering if it has anything to do with that?
script in issue: (Not local script, located within StarterCharacterScripts)
local player = game.Players:GetPlayerFromCharacter(script.Parent)
local team = game:GetService("Teams")['banker']
local civCheck = 50
local bankCheck = 150
local cash = player.leaderstats.cash.Value
while true do
if player.Team == team then
cash = cash + bankCheck
print(cash)
else
cash = cash + civCheck
print(cash)
end
wait(5)
end
data save script: (Not local script, located within ServerScriptService) Note: this is only the data save portion of the scrript
local plrID = "Player_" .. player.UserId
local data = DataStore:GetAsync(plrID)
if data then
moolah.Value = data['cash']
C4Count.Value = data['count']
owned.Value = data['owned']
else
moolah.Value = 1000
C4Count.Value = 0
owned.Value = false
end
player.CharacterAdded:Connect(function(char)
char.Humanoid.Died:Connect(function(Died)
local creator = char.Humanoid:FindFirstChild("creator")
local leaderstats = creator.Value:FindFirstChild("leaderstats")
if creator ~= nil and creator.Value ~= nil then
time.Value = 30
player.leaderstats.Wanted.Value = true
end
end)
end)
end
game.Players.PlayerAdded:Connect(cashhandle)
game.Players.PlayerRemoving:Connect(function(player)
local dataToSave = {
count = player.count.Value,
cash = player.leaderstats.cash.Value,
owned = player.bikeOwned.Value
}
local plrID = "Player_" .. player.UserId
local success, err = pcall(function()
DataStore:SetAsync(plrID, dataToSave)
end)
if success then
print("Data Saved!")
else
print("Data Save Error!!")
end
end)

2 comments
Data like that should always be changed on the server. Also, when something is done on the client (StarterCharacterScripts or StarterPlayerScripts) it should be run in a local script.
If you want to request some data be changed from the client, you should use a RemoteEvent and handle the request on the server.