Hello, i have a problem that i can't seem to figure out, and this has been a problem for quite a while now, but i decided to ask for help this time!
Anyways, here's the whole script and where everything is located.
This Script Is Located In game.ServerScriptService.
-- DataStoring Two IntValues
--Everything works fine with this code, nothing wrong with it, just going to put it here incase there has to be anything changed.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStoreNew")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = leaderstats
-- Load Data
local playerUserId = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Cash.Value = data
XP.Value = data
print("Player data successfully loaded!")
else
print("There was an error whilst loading player data!")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local cashData = player.leaderstats.Cash.Value
local xpData = player.leaderstats.XP.Value
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, cashData, xpData)
end)
if success then
print("Player data successfully saved!")
else
print("There was an error whilst saving player data!")
warn(errormessage)
end
end)
This Script Is Located In : game.StarterPlayer.StarterPlayerScripts.
local player = game.Players.LocalPlayer
wait(3) -- Making sure the player has loaded otherwise an error will pop up
if player.leaderstats.XP.Value == 10 then
print("Player has successfully reached 10 XP!")
end
Thanks for reading through! Hope you can resolve this issue that i'm having.

2 comments
You can also do repeat wait() until player and player.Character to wait for the player to load then use changed event to check instead of loop...
or do: while player.leaderstats.XP.Value <= 10 and wait() do print("Player has successfully reached 10 XP!") break end