I've made a datastore that saves money. The problem here is that when I click the button that gives me the money, the money gets added but when I exit and rejoin the money isn't there. I tested about adding the value through properties and the datastore saves that value. Here are the Scripts:
Leaderstats (script)
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 local coins = Instance.new("IntValue") coins.Name = "Coins" coins.Parent = leaderstats local data local success, errorMessage = pcall(function() data = myDataStore:GetAsync(player.UserId) end) if success then coins.Value = data else warn(errorMessage) end end) game.Players.PlayerRemoving:Connect(function(player) local success, errorMessage = pcall(function() local data = myDataStore:SetAsync(player.UserId, player.leaderstats.Coins.Value) end) if success then print("Data Saved") else warn(errorMessage) end end) game:BindToClose(function() for i, player in pairs(game.Players:GetChildren()) do player:Kick("Server Closed") end wait(2) end)
Button (localscript)
local Player = game.Players.LocalPlayer local PlayerMoney = Player:WaitForChild('leaderstats'):WaitForChild('Coins') script.Parent.MouseButton1Click:Connect(function() PlayerMoney.Value += 1 end)
API is enabled too. I hope somebody helps me. Thank you in advance.
local key=""..player.UserId -- it needs to be a string