I’m trying to learn how to script better and I figured the best way to do that would be to try something new. So I’m trying to make a somewhat simple datastore for xp using a youtube video as reference. Everything works perfectly fine but the data doesn’t save or load in when I rejoin. Like I said I’m new to datatstores so I haven’t tried anything and I don’t know what to try to fix this issue. Any help would be much appreciated. Also both scripts are located in ServerScriptService and are regular scripts.
This is the script that handles the datastore side of things.
local DataStoreService = game:GetService("DataStoreService")
local XPStore = DataStoreService:GetDataStore("XPStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
local UserId = Player.UserId
local XPData = XPStore:GetAsync(UserId)
if XPData == nil then
XPData = 0
XPStore:SetAsync(UserId, XPData)
end
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local XP = Instance.new("IntValue", leaderstats)
XP.Name = "XP"
XP.Value = XP
XP.Changed:Connect(function(NewValue)
XPStore:SetAsync(UserId, NewValue)
end)
end)
This script is what gives the xp.
local Players = game:GetService("Players")
Players.PlayerAdded:connect(function(Player)
repeat
wait(60)
local XP = Player.leaderstats.XP
XP.Value = XP.Value + 10
until
nil
end)
You should be saving on .PlayerRemoved, :BindToClose and if you want to, you may occasionally auto save. However, saving the data every time it is changed may cause some throttling with your data store. Also, there are some lines that need to be changed