This data store script doesn't work for some reason. Can you guys check it out please. I have put print()
after if nil then
and it did not print, this means that the data store is saving but it's not saving the right value.
Script location = ServerScriptService
local xp = game:GetService("DataStoreService"):GetDataStore("XP") local function Xp(key, newValue) local suc, res = pcall(function() return xp:SetAsync(key, newValue) end) if suc then return true end end game:GetService('Players').PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) local experience = Instance.new('IntValue') experience.Name = 'XP' experience.Value = xp:GetAsync(player.UserId) if nil then -- I put the print() in here experience.Value = 0 end while wait(30) do Xp(player.UserId, player.XP.Value) end end) end)
You forgot to set the parent, I gave some modified. errors: Parent not set. You can not put if nil then you must specify the variable / object. You do not need to use player.CharacterAdded. It is not necessary to create a save function.
If it does not work in Roblox Studio you must enable the API, go to the page of your place, go to Configure Game and enable Studio Access to API Services, click save and that's it.
Here is the script done:
local xp = game:GetService("DataStoreService"):GetDataStore("XP") local startvalue = 0 -- Set start XP game:GetService('Players').PlayerAdded:Connect(function(player) local async = xp:GetAsync(player.UserId) -- Get Saved Value local leaderstats = Instance.new('IntValue', player) -- Create a leaderstats leaderstats.Name = 'leaderstats' local experience = Instance.new('IntValue', leaderstats) -- Create XP value experience.Name = 'XP' if async then -- If found save then experience.Value = xp:GetAsync(player.UserId) -- Get XP Saved else experience.Value = startvalue -- Set start value wait() xp:SetAsync(player.UserId, experience.Value) -- Save XP end experience.Changed:Connect(function() -- If XP changed save value xp:SetAsync(player.UserId, experience.Value) -- Save XP end) end)
If you want to make some changes you can do, however it is not recommended to add a loop to save the value.
Remember, for save you need to change value with server, for this use RemoteEvent