So I’m currently working on a level system for my game, but I’ve run into a problem while scripting the datastore for it. Instead of the amount of XP needed to level up is at 100, if the player hadn’t leveled up from level 1 and level value will be at 1, but the amount of XP needed to level up will only go as 0 instead of 100 and sometimes the other values will tend to turn to 0 including the player’s level. I would be able to level up still but I wanted the level to be 1 instead 0 once the player joins without any data. Here’s an Image:
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("LevelStatSave")
local data
local function createTable(player) --This will create a table to hold all the level stats
local level_stats = {}
for _, stat in pairs(player.LevelStats:GetChildren())do
level_stats[stat.Name] = stat.Value
end
return level_stats
end
game.Players.PlayerAdded:Connect(function(player) --Once the player joined it will run the event
local LevelStats = Instance.new("Folder", player) --Will create a folder the will hold all the stats
LevelStats.Name = "LevelStats"
local level = Instance.new("IntValue", LevelStats) --Will create the player's level
level.Name = "Level"
local exp = Instance.new("IntValue", LevelStats) --Will create the player's current XP
exp.Name = "Current"
local maxExp = Instance.new("IntValue", LevelStats) --Will create the player's Max XP they
maxExp.Name = "Max"
--DataStore Load
data = datastore:GetAsync(player.UserId)
if data then
level.Value = data["Level"]
exp.Value = data["Current"]
maxExp.Value = data["Current"]
else
level.Value = 1
exp.Value = 0
maxExp.Value = 100
end
end)
--DataStore Save
game.Players.PlayerRemoving:Connect(function(player)
local level_stats = createTable(player)
local success, err = pcall(function()
datastore:SetAsync(player.UserId, level_stats)
end)
if not success then
warn("Failed to save Level Stats!")
end
end)
Does anyone know how to help me out with this, I’m still new to data stores so I’m not that experienced. Any help will be appreciated!
Did you mean to put the maxExp the same as the data["Current"]…? Also I’d just add a local data inside the PlayerAdded event, not sure why you’re putting it outside
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("LevelStatSave")
local function createTable(player) --This will create a table to hold all the level stats
local level_stats = {}
for _, stat in pairs(player.LevelStats:GetChildren())do
level_stats[stat.Name] = stat.Value
end
return level_stats
end
game.Players.PlayerAdded:Connect(function(player) --Once the player joined it will run the event
local LevelStats = Instance.new("Folder", player) --Will create a folder the will hold all the stats
LevelStats.Name = "LevelStats"
local level = Instance.new("IntValue", LevelStats) --Will create the player's level
level.Name = "Level"
local exp = Instance.new("IntValue", LevelStats) --Will create the player's current XP
exp.Name = "Current"
local maxExp = Instance.new("IntValue", LevelStats) --Will create the player's Max XP they
maxExp.Name = "Max"
local data
--DataStore Load
data = datastore:GetAsync(player.UserId)
if data then
level.Value = data["Level"]
exp.Value = data["Current"]
maxExp.Value = data["Max"]
else
level.Value = 1
exp.Value = 0
maxExp.Value = 100
end
end)
--DataStore Save
game.Players.PlayerRemoving:Connect(function(player)
local level_stats = createTable(player)
local success, err = pcall(function()
datastore:SetAsync(player.UserId, level_stats)
end)
if not success then
warn("Failed to save Level Stats!")
end
end)
Not quite
, the maxExp is how much XP the player needs to level up and the current is the amount of XP the player has currently, and imma try putting the local data inside the function.
What they mean is did you click this and then change the value. And if you didn’t that means you’re still on client side and the server can’t save data from that.
yes, and since the level system script is supposed to the times the max exp at a certain number depending the player’s level, 0 * something = 0. so it’ll always stay at 0 no matter what.
Just realized that you should probably encase your GetAsync function inside a pcall as well oof
Try this and see what outputs back:
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("LevelStatSave")
local function createTable(player) --This will create a table to hold all the level stats
local level_stats = {}
for _, stat in pairs(player.LevelStats:GetChildren())do
level_stats[stat.Name] = stat.Value
end
return level_stats
end
game.Players.PlayerAdded:Connect(function(player) --Once the player joined it will run the event
local LevelStats = Instance.new("Folder", player) --Will create a folder the will hold all the stats
LevelStats.Name = "LevelStats"
local level = Instance.new("IntValue", LevelStats) --Will create the player's level
level.Name = "Level"
local exp = Instance.new("IntValue", LevelStats) --Will create the player's current XP
exp.Name = "Current"
local maxExp = Instance.new("IntValue", LevelStats) --Will create the player's Max XP they
maxExp.Name = "Max"
local data
--DataStore Load
local success, whoops = pcall(function()
data = datastore:GetAsync(player.UserId)
end)
if success and data then
level.Value = data["Level"]
exp.Value = data["Current"]
maxExp.Value = data["Max"]
else
warn(whoops)
level.Value = 1
exp.Value = 0
maxExp.Value = 100
end
end)
--DataStore Save
game.Players.PlayerRemoving:Connect(function(player)
local level_stats = createTable(player)
print(level_stats)
local success, err = pcall(function()
datastore:SetAsync(player.UserId, level_stats)
end)
if not success then
warn("Failed to save Level Stats!")
end
end)