DataStore Issues

I have this datastore but because of {

CurrentXP.Changed:Connect(function(val)
if CurrentXP.Value >= MaxXP.Value then
Lv.Value += 1
CurrentXP.Value = 0
MaxXP.Value *= 1.25
end
end)

} The CurrentXP.Value won’t save, If anyone can help me fix this issue I would greatly appreciate it!

local DataStore = game:GetService(“DataStoreService”):GetDataStore(“Store”)

game.Players.PlayerAdded:Connect(function(Player)

local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player

local Currency = Instance.new("IntValue")
Currency.Name = "Money"
Currency.Parent = Leaderstats

local Lv = Instance.new("IntValue")
Lv.Name = "Level"
Lv.Parent = Leaderstats

local CurrentXP = Instance.new("IntValue")
CurrentXP.Name = "Current"
CurrentXP.Parent = Lv

local MaxXP = Instance.new("IntValue")
MaxXP.Name = "Max"
MaxXP.Parent = Lv

**CurrentXP.Changed:Connect(function(val)**

** if CurrentXP.Value >= MaxXP.Value then**
** Lv.Value += 1**
** CurrentXP.Value = 0**
** MaxXP.Value = 1.25*
** end**
** end)**

local CurrencyData
local LvData
local CurrentData
local MaxData

local Success, ErrorMessage = pcall(function()
	CurrencyData = DataStore:GetAsync(Player.UserId.."-CurrencyData")
	LvData = DataStore:GetAsync(Player.UserId.."-LvData")
	CurrentData = DataStore:GetAsync(Player.UserId.."-CurrentData")
	MaxData = DataStore:GetAsync(Player.UserId.."-MaxData")
end)

if not Success then
	print(ErrorMessage)
end

if CurrencyData ~= nil then
	Currency.Value = CurrencyData
else
	Currency.Value = 0
end

if LvData ~= nil then
	Lv.Value = LvData
else
	Lv.Value = 1
end

if CurrentData ~= nil then
	CurrentXP.Value = CurrentData
else
	CurrentXP.Value = 0
end

if MaxData ~= nil then
	MaxXP.Value = MaxData
else
	MaxXP.Value = 100
end

end)

game.Players.PlayerRemoving:Connect(function(Player)
pcall(function()
DataStore:SetAsync(Player.UserId…"-CurrencyData", Player.leaderstats.Money.Value)
DataStore:SetAsync(Player.UserId…"-LvData", Player.leaderstats.Level.Value)
DataStore:SetAsync(Player.UserId…"-CurrentData", Player.leaderstats.Level.Current.Value)
DataStore:SetAsync(Player.UserId…"-MaxData", Player.leaderstats.Level.Max.Value)
end)
end)

Can you show your whole script, so it’s much easier for me to read.

Whole script is between 2 ``` please

How Can I show the whole script?

Nvm, are you testing in studio?

Yes, I am testing in studio…

Are you enabling studio access to api services?

Yes they are enabled, The problem is when I put this inside the script

CurrentXP.Changed:Connect(function(val)
	if CurrentXP.Value >= MaxXP.Value then
		Lv.Value += 1
		CurrentXP.Value = 0
		MaxXP.Value *= 1.25
	end
end)

Is there any errors in the output?

No, Everything works fine, If you can tell me how to show you the whole thing I’m sure it will make everything easier.

Here is A link to the game: Zombie Apocalypse - Roblox

When you shoot the zombies they give xp every time you shoot them but, when you leave and rejoin it does not save.

I think I know what is going on. You’re put the event before everything is loaded in. So as soon they’re loaded in, the event emitted. So put it after every data is loaded.

It works now put gives me an error message

20:46:43.941 ServerScriptService.NewDataStore:76: attempt to index nil with ‘Changed’ - Server - NewDataStore:76
20:46:43.941 Stack Begin - Studio
20:46:43.941 Script ‘ServerScriptService.NewDataStore’, Line 76 - Studio - NewDataStore:76
20:46:43.941 Stack End - Studio

Can I see where you put the event?

local DataStore = game:GetService(“DataStoreService”):GetDataStore(“Store”)

game.Players.PlayerAdded:Connect(function(Player)

local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player

local Currency = Instance.new("IntValue")
Currency.Name = "Money"
Currency.Parent = Leaderstats

local Lv = Instance.new("IntValue")
Lv.Name = "Level"
Lv.Parent = Leaderstats

local CurrentXP = Instance.new("IntValue")
CurrentXP.Name = "Current"
CurrentXP.Parent = Lv

local MaxXP = Instance.new("IntValue")
MaxXP.Name = "Max"
MaxXP.Parent = Lv

local CurrencyData
local LvData
local CurrentData
local MaxData

local Success, ErrorMessage = pcall(function()
	CurrencyData = DataStore:GetAsync(Player.UserId.."-CurrencyData")
	LvData = DataStore:GetAsync(Player.UserId.."-LvData")
	CurrentData = DataStore:GetAsync(Player.UserId.."-CurrentData")
	MaxData = DataStore:GetAsync(Player.UserId.."-MaxData")
end)

if not Success then
	print(ErrorMessage)
end

if CurrencyData ~= nil then
	Currency.Value = CurrencyData
else
	Currency.Value = 0
end

if LvData ~= nil then
	Lv.Value = LvData
else
	Lv.Value = 1
end

if CurrentData ~= nil then
	CurrentXP.Value = CurrentData
else
	CurrentXP.Value = 0
end

if MaxData ~= nil then
	MaxXP.Value = MaxData
else
	MaxXP.Value = 100
end

end)

game.Players.PlayerRemoving:Connect(function(Player)
pcall(function()
DataStore:SetAsync(Player.UserId…"-CurrencyData", Player.leaderstats.Money.Value)
DataStore:SetAsync(Player.UserId…"-LvData", Player.leaderstats.Level.Value)
DataStore:SetAsync(Player.UserId…"-CurrentData", Player.leaderstats.Level.Current.Value)
DataStore:SetAsync(Player.UserId…"-MaxData", Player.leaderstats.Level.Max.Value)
end)
end)

CurrentXP.Changed:Connect(function(val)
** if CurrentXP.Value >= MaxXP.Value then**
** Lv.Value += 1**
** CurrentXP.Value = 0**
** MaxXP.Value = 1.25*
** end**
end)

Can anyone help me fix this issue?

Solved The Issue, I had to move the CurrentXP.Changed:Connect(function(val)

right under {

if MaxData ~= nil then
MaxXP.Value = MaxData
else
MaxXP.Value = 100
end

}

Lol, you must put it after every data loaded I said. Not after PlayerAdded :laughing:

1 Like