I was doing a cash system, I used datastoreservice but it seems to no save and always be 0 for some reason.
Game 1:
Game 2:
local DataStoreService = game:GetService("DataStoreService")
local CashStore = DataStoreService:GetDataStore("CashStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
local CashValue = Instance.new("IntValue")
CashValue.Name = "Cash"
CashValue.Parent = Player
local Success, Error = pcall(function()
local PlayerCash = CashStore:GetAsync(Player.UserId)
print(PlayerCash)
if PlayerCash then
CashValue.Value = PlayerCash
end
end)
if Error then
Player:Kick("Your Data did not load, Please try again")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Success, Error = pcall(function()
CashStore:SetAsync(Player.UserId,Player.Cash.Value)
end)
end)
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
local Success, Error = pcall(function()
CashStore:SetAsync(Player.UserId,Player.Cash.Value)
end)
end
end)
First of all, do something like this at the end of the script:
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player) --Replace the existing saving part of your script with a function
end
end)
Edit: I also noticed that you aren’t defining the variable properly. It should look something like this:
local PlayerCash --Add this to your script and remove the local two lines down
local Success, Error = pcall(function()
PlayerCash = CashStore:GetAsync(Player.UserId)
end)
if PlayerCash and Success then
print(PlayerCash)
CashValue.Value = PlayerCash
elseif Error then
warn(Error)
end
end)