And the only thing the seems to be holding me back is the DataStoreService so basically everytime I tried to save cash it always print in the output cash has been saved but everytime when I join back the cash isn’t saved and I’ve tried anything so I decided to reach out to you guys anyone mind giving me an helping hand code is below
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
local key = "Player_" .. player.UserId
local leaderboard = Instance.new("Folder")
leaderboard.Name = "leaderstats"
leaderboard.Parent = player
local Coin = Instance.new("IntValue")
Coin.Name = "Coin"
Coin.Parent = leaderboard
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = leaderboard
local Rebirth = Instance.new("IntValue")
Rebirth.Name = "Rebirth"
Rebirth.Parent = leaderboard
local Data
local success, errormessage = pcall(function()
Data = DataStoreService:GetAsync(key)
end)
if success then
Coin.Value = Data
print("Success!")
else
print("Unsuccess!")
warn(errormessage)
end
game.Players.PlayerRemoving:Connect(function(player)
local key = "Player_" .. player.UserId
local Data = player.leaderstats.Coin.Value
local Data = player.leaderstats.XP.Value
local Data = player.leaderstats.Rebirth.Value
local success, errormessage = pcall(function()
Data = DataStoreService:SetAsync(key, Data)
end)
if success then
print("Success!")
else
print("Unsuccess!")
warn(errormessage)
end
end)
end)
local PlayerData = {}
game:BindToClose(function()
if RunService:IsStudio() then
return
end
print("Saving All Player Data...")
local players = Players:GetPlayers()
for _, player in pairs(Players) do
local key = player.UserId
local Data = PlayerData[key]
if Data then
local success, errormessage = pcall(function()
DataStoreService:SetAsync(key, Data)
end)
if not success then
warn(errormessage)
end
end
end
print("Completed saving player Data")
end)```
If this is for your game… of course it wont work. You said if in studio the game is checking to see if you are in studio. You are not. You are in your game. So you could create an else statement to check if you are not in studio to save data.
local Data = {player.leaderstats.Coin.Value, player.leaderstats.XP.Value, player.leaderstats.Rebirth.Value}
local success,errormessage = pcall(function()
DataStoreService:SetAsync(key,Data)
end)
Here’s a way you can make a dictionary of the values you’re saving (Coin / XP / Rebirth)
game.Players.PlayerRemoving:Connect(function(player)
local key = "Player_" .. player.UserId
local Data_Table = {} --I will name the table Data_DiningRoomTable beyond this point replace it
Data_DiningRoomTable.Coin = player.leaderstats.Coin.Value
Data_DiningRoomTable.XP = player.leaderstats.XP.Value
Data_DiningRoomTable.Rebith = player.leaderstats.Rebirth.Value
print('Dictionary being stored',Data_Table)
local success, errormessage = pcall(function()
Data = DataStoreService:SetAsync(key, Data_Table)
end)
if success then
print("Success!")
else
print("Unsuccess!")
warn(errormessage)
end
end)