I have a script that is meant to save, but doesn’t.
--//Level System\\--
local ds = game:GetService("DataStoreService")
local level = ds:GetDataStore("Level")
local exp = ds:GetDataStore("Exp")
game.Players.PlayerAdded:Connect(function(plr)
local lvl = Instance.new("IntValue",plr:WaitForChild("leaderstats"))
local xp = Instance.new("IntValue",plr:WaitForChild("leaderstats"))
print("Creating Values")
xp.Name = "Exp"
lvl.Name = "Level"
xp.Value = exp:GetAsync(plr.UserId) or 0
lvl.Value = level:GetAsync(plr.UserId) or 1
xp.Changed:Connect(function()
if xp.Value >= lvl.Value*100 then
lvl.Value = lvl.Value+1
local hi = lvl.Value * 100
local extraXP = xp.Value - hi
xp.Value = extraXP
end
if xp.Value < 0 then
xp.Value = 0
end
end)
plr.Character.Humanoid.Died:Connect(function()
exp:SetAsync(plr.UserId,xp.Value)
level:SetAsync(plr.UserId,lvl.Value)
print("died, saving values")
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
print("Removing")
local xp = plr.leaderstats.Exp
local lvl = plr.leaderstats.Level
exp:SetAsync(plr.UserId,xp.Value)
level:SetAsync(plr.UserId,lvl.Value)
print("left, saving values")
end)
The only things I get in the output are “Creating Values”, and “Removing”
What is wrong?
*also, values are meant to save when the player dies or leaves the game
Nothing after your while wait() do loop will be executed inside of your PlayerAdded (e.g your listener for a player dying). I would change it to listen to .Changed on xp and lvl.
Also, your Character.Died listener will only be called once - you should put it inside of a plr.CharacterAdded to ensure that it listen for a player dying after they respawn.
Your “.Died” event will not connect because the while loop is above it. This is an indefinite loop that will not stop, meaning all code below it won’t run.
There is no “.Died” event in the player’s character model, you’re looking for plr.Character.Humanoid.Died instead.
I will also mention that the way you’re handling levelling isn’t super efficient. It would be extremely more efficient to make a global function that awards XP and automatically sorts out levelling up.
Also, make sure that studio has access to api services. If it doesn’t then data won’t be able to save. You can change this option by going to the Configure Game page and enabling it (should be on the first page)
In which case, please refer to the rest of my post and give that a try. I can reproduce your intended behaviour perfectly fine in studio with the exact code in your post.
I’ve reworked your script a little, this should work correctly.
--//Level System\\--
local ds = game:GetService("DataStoreService")
local level = ds:GetDataStore("Level")
local exp = ds:GetDataStore("Exp")
local function handleCharacter(char)
local plr=game:GetService("Players"):GetPlayerFromCharacter(char)
char:WaitForChild("Humanoid").Died:connect(function ()
exp:SetAsync(plr.UserId,plr.leaderstats.Exp.Value)
level:SetAsync(plr.UserId,plr.leaderstats.Level.Value)
print("died, saving values")
end)
end
game.Players.PlayerAdded:Connect(function(plr)
Instance.new("Folder",plr).Name="leaderstats"
local lvl = Instance.new("IntValue",plr:WaitForChild("leaderstats"))
local xp = Instance.new("IntValue",plr:WaitForChild("leaderstats"))
plr.CharacterAdded:connect(handleCharacter)
if plr.Character then
handleCharacter(plr.Character)
end
print("Creating Values")
xp.Name = "Exp"
lvl.Name = "Level"
xp.Value = exp:GetAsync(plr.UserId) or 0
lvl.Value = level:GetAsync(plr.UserId) or 1
xp.Changed:Connect(function()
if xp.Value >= lvl.Value*100 then
lvl.Value = lvl.Value+1
local hi = lvl.Value * 100
local extraXP = xp.Value - hi
xp.Value = extraXP
end
if xp.Value < 0 then
xp.Value = 0
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
print("Removing")
local xp = plr.leaderstats.Exp
local lvl = plr.leaderstats.Level
exp:SetAsync(plr.UserId,xp.Value)
level:SetAsync(plr.UserId,lvl.Value)
print("left, saving values")
end)