- My issue is: I don’t know if the data store, I made is working but anytime the data is loaded, the level and cash double or tripled. The data will save just level 5 and 500 cash, as shown in this gif https://i.imgur.com/TQZGurr.gif
- However, it loads level 7 and 700 cash.
https://i.imgur.com/AsS33iK.gif
- I tried some solution in the dev hub and yt.
I’m creating a gui level system in which the level increases and cash when it reaches the required xp.
Here is my leaderstats and data store.
local DataStoreSer = game:GetService("DataStoreService")
local PlayerData = DataStoreSer:GetDataStore("DataTestxdsds")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderboard"
local Cash = Instance.new("NumberValue", leaderstats)
Cash.Name = "Cash"
local Level = Instance.new("NumberValue", leaderstats)
Level.Name = "Level"
local XP = Instance.new("NumberValue", leaderstats)
XP.Name = "XP"
local ReqXP = Instance.new("NumberValue", Player)
ReqXP.Name = "ReqXP"
ReqXP.Value = Level.Value*5
XP.Changed:Connect(function(Changed)
if XP.Value >= ReqXP.Value then
XP.Value = 0
Level.Value += 1
ReqXP.Value = Level.Value*5
Cash.Value += 100
end
end)
-- Data
local playerUserID = 'Player'..Player.UserId
local data = PlayerData:GetAsync(playerUserID)
if data then
print("Data found. [DATA LOADED]")
Cash.Value = data["Cash"]
Level.Value = data["Level"]
XP.Value = data["XP"]
else
print("No data found. [NEW PLAYER]")
Cash.Value = 0
Level.Value = 0
XP.Value = 0
end
end)
local function create_table(Player)
local player_stats = {}
for _, stat in pairs(Player.leaderboard:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
game.Players.PlayerRemoving:Connect(function(Player)
local player_stats = create_table(Player)
local success, err = pcall(function()
local playerUserID = 'Player'..Player.UserId
PlayerData:SetAsync(playerUserID, player_stats)
end)
if success then
print("Your data successfully saved!")
else
warn("Could not save your data.")
end
end)