So i have this script which is supposed to make a leaderstat and 2 other IntValues in the player, and it does this really well but the Datastore part does not save the XP and XPNeeded (The 2 values which are not in the leaderstats), But it saves the Levels even when im in studio. Why is this and how do i fix it?
The script:
local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("Level")
local ds2 = DataStore:GetDataStore("XP")
local ds3 = DataStore:GetDataStore("XPNeeded")
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("IntValue", player)
xp.Name = "XP"
xp.Value = ds2:GetAsync(player.UserId) or 0
ds2:SetAsync(player.UserId.."-XP", xp.Value)
------------------------------------------
local XPN = Instance.new("IntValue", player)
XPN.Name = "XPNeeded"
XPN.Value = ds3:GetAsync(player.UserId) or 1000
ds3:SetAsync(player.UserId.."-XPNeeded", XPN.Value)
xp.Changed:connect(function()
print("+XP SAVING")
if xp.Value >= XPN.Value then
player.leaderstats.Level.Value = player.leaderstats.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
ds3:setAsync(player.UserId, player.XPNeeded.Value)--XPNeeded
print("SavingData")
print("Player Left")
SavedData:Fire()
game:BindToClose(function()
SavedData.Event:Wait()
end)
end)
end)
end)
end)
Im guessing the issue is in the saving part because when i run the game the "+XP Saving" print does get printed out and the "Player left" at the end does not.

1 comment
It is on