Having problems with a datastore

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)
2 Likes

It looks like you’re not setting the XP’s value to the XPData

Maybe try replacing the XP here with XPData

1 Like

This can be a

while wait(60) do
local XP = Player.leaderstats.XP
XP.Value + 10
end
2 Likes

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

Anyways, I hope this helps :slight_smile:

1 Like

First don’t do this:

Instead of this you should make that data saves when player leaves.

Secondly the Script that gives XP should look like this (I don’t know if it does work in right way- it wasn’t tested):

game:GetService("Players").PlayerAdded:Connect(function(player)
repeat
wait(60)
player.leaderstats:WaitForChild("XP").Value = player.leaderstats:WaitForChild("XP").Value + 10
until player == nil
end)

I haven’t looked at the Main Script, but it should be good.

Edit: Main Scipt is not good actually. Replace this:

Into this:

local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Value = XPData
XP.Parent = leaderstats
1 Like

Thank you for the information I made all the changes you suggested and the data now loads however I’m still having an issue with saving.

This is what I added for saving

Players.PlayerRemoving:Connect(function(PlayerExiting)
		XPStore:SetAsync(UserId, XPData)
1 Like

Change that line to:

XPStore:SetAsync(PlayerExiting.UserId, PlayerExiting.leaderstats.XP.Value)

This should work for saving:

game:GetService("Players").PlayerRemoving:Connect(function(player)
XPStore:SetAsync(player.UserId, player:WaitForChild("leaderstats"):WaitForChild("XP").Value)
end)
2 Likes

Thank you that worked, it appears everything is now working as intended.

2 Likes