I'm pretty sure this is the problem. I made a datastore script that goes like this:
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Display an 'IntValue' on leaderboard
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = MyDataStore:GetAsync(player.UserId.."-cash") or 0
end)
if success then
cash.Value = data
else
print("error")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
MyDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
end)
if success then print("Data Saved")
else print("error")
warn("errormessage")
end
end)
If I play my game on Roblox (not studio) I am able to save the value of cash if I type a command from HD admin.
Change me Cash 1000
I leave the game and this is saved and remains when I come back. However, I have a localscript in StarterPlayerScripts that is:
while true do
wait(10)
game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value + 10
end
The 1000 will go (ex.1040) and it won't save.
I can use the admin command to change the value to 1040 and it saves.
What can I do to fix this?
