So i have made this script which is a Level up system integrated with the datastore system, now the datastore works well but the Level up part Line 26 - 32 does not. What is the issue? and how do i fix it?
the code:
local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("Level")
local ds2 = DataStore:GetDataStore("XP")
local ds3 = DataStore:GetDataStore("XPN")
local success, err = pcall(function()
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder", player)
leader.Name = "leaderstats"
------------------------------------------
local level = Instance.new("IntValue", leader)
level.Name = "Level"
level.Value = ds1:GetAsync(player.UserId) or 1
ds1:SetAsync(player.UserId.."-Level", level.Value)
------------------------------------------
local xp = Instance.new("NumberValue", player)
xp.Name = "XP"
xp.Value = ds2:GetAsync(player.UserId) or 1
ds2:SetAsync(player.UserId.."-XP", xp.Value)
------------------------------------------
local XPN = Instance.new("IntValue", player)
XPN.Name = "XPNeeded"
XPN.Value = ds3:GetAsync(player.UserId) or 1000
ds2:SetAsync(player.UserId.."-XPNeeded", XPN.Value)
xp.Changed:connect(function()
print("+XP SAVING")
if player:WaitForChild("XP").Value >= player:WaitForChild("XpNeeded").Value then
level.Value = level.Value+1
XPN.Value = XPN.Value + 500
XPN.Value = XPN.Value
xp.Value = 0
ds1:SetAsync(player.UserId.."-Level", level.Value)
ds2:SetAsync(player.UserId.."-XP", xp.Value)
ds3:SetAsync(player.UserId.."-XPNeeded", XPN.Value)
else
ds1:SetAsync(player.UserId.."-Level", level.Value)
ds2:SetAsync(player.UserId.."-XP", xp.Value)
ds3:SetAsync(player.UserId.."-XPNeeded", XPN.Value)
end
game.Players.PlayerRemoving:connect(function(player)
local SavedData = Instance.new("BindableEvent")
ds1:setAsync(player.UserId, player.leaderstats.Level.Value)--Level
ds2:setAsync(player.UserId, player.xp.Value)--XP
ds2:setAsync(player.UserId, player.XPNeeded.Value)--XPNeeded
print("SavingData")
SavedData:Fire()
game:BindToClose(function()
SavedData.Event:Wait()
end)
end)
end)
end)
end)

2 comments
What is the output?
Output is clear