Datastore2's OnUpdate function isnt working for me?

I just want to mention that most of my posts on the devforum are about datastores, so I guess that kinda shows something about me.

So I’m using @Kampfkarren’s datastore2 module (latest version from github) and I have this script where a player has xp, and when they get xp their datastore updates. When they reach enough exp they level up and gain more max health. Now the script changes the datastore and that change allows the value inside the player to change as well. However, in my script the value inside the player only changes when they enter. Using print statements, I realized this was because the :OnUpdate was only firing when the player first enters the game. For those of you more familar with datastore2, can you let me know if there’s any mistakes in this script?

local sss = game:GetService("ServerScriptService")
local Datastore2 = require(sss.DataStore2)

Datastore2.Combine("MasterKey","Level","XP","MaxHealth")

local defaultlevel = 1
local defaultxp = 0
local defaulthealth = 100
local defaultmaxhealth = 100

local xpToLevelUp = function(level)
	print(50*level*(1+level))
	return 50*level*(1+level) 
end

game.Players.PlayerAdded:Connect(function(player)
	local values = Instance.new("Folder", player); values.Name = "Values"
	
	local levelValue = Instance.new("IntValue", values); levelValue.Name = "Level"
	local xpValue = Instance.new("IntValue", values); xpValue.Name = "XP"
	local healthValue = Instance.new("IntValue", values); healthValue.Name = "Health"
	local maxhealthValue = Instance.new("IntValue", values); maxhealthValue.Name = "MaxHealth"
	
	local levelstore = Datastore2("Level",player)
	local xpstore = Datastore2("XP",player)
	local maxhealthstore = Datastore2("MaxHealth",player)
	
	local function updatelevel(level)
		if level ~= nil then
		player.Values.Level.Value = level
			if level ~= 1 then
				
			maxhealthstore:Increment(10)
			end
		end
	end
	
	local function updateMaxHealth(maxhealth)
		print("so: " .. maxhealth)
		player.Values.MaxHealth.Value = maxhealth
		player.Values.Health.Value = maxhealth
	end
	
	local function updateXP(xp)
		if xp ~= nil then
		if xp >= xpToLevelUp(levelstore:Get(defaultlevel)) then
			print("leveled up!")
			xpstore:Increment(xpToLevelUp(levelstore:Get(defaultlevel)) * -1)
			levelstore:Increment(1)
		else
				player.Values.XP.Value = xp
				print("not enough xp: " .. player.Values.XP.Value)
				end
		end
	end
	
	
	updatelevel(levelstore:Get(defaultlevel))
	updateXP(xpstore:Get(defaultxp))
	updateMaxHealth(maxhealthstore:Get(defaultmaxhealth))
	
	levelstore:OnUpdate(updatelevel())
	xpstore:OnUpdate(updateXP())
	maxhealthstore:OnUpdate(updateMaxHealth())
end) 

Not sure if this matters, but I have a part with a click detector that fires a remote event to increment the datastore. Heres the code for that (the print statement shows up so I know the datastore is changing)

local sss = game:GetService("ServerScriptService")
local Datastore2 = require(sss.DataStore2)
game.Workspace.remoteevents.test.OnServerEvent:Connect(function(player)
	local xpstore = Datastore2("XP",player)
	xpstore:Increment(10)
	print(xpstore:Get())
end)
1 Like

Change this:

To this:

levelstore:OnUpdate(updatelevel) 
xpstore:OnUpdate(updateXP) 
maxhealthstore:OnUpdate(updateMaxHealth)

By adding the parenthesis, you’re trying to call the updateXP function without passing an argument (updateXP() is essentially updateXP(nil)).

xpstore:OnUpdate(updateXP) will automatically pass the inherited argument.

4 Likes

i’m running into this same issue and this explanation doesn’t make sense. “change this, to this” and the two appear to be identical- what is the difference?

Oh wow, thanks for noticing this. Looks like I had a typo in the solution. I edited the post to show the correct code.

The explanation under it is still correct, when you want to call a function from some listener function you don’t want to add the () at the end because the inherited arguments won’t pass.

Hello, I have the same issue like this where it doesn’t update but only works when I just rejoin but I don’t have the () on mine. I have like a slot system. Heres how it works:

Default Values:

local defaultEquippedSlots = {
	GearSlot1 = "";
	GearSlot2 = "";
	GearSlot3 = "";
	PerkSlot = "";
}

Table of DataStores

local slotsStore = {
		GearSlot1 = DataStore2("GearSlot1", player),
		GearSlot2 = DataStore2("GearSlot2", player),
		GearSlot3 = DataStore2("GearSlot3", player),
		PerkSlot =  DataStore2("PerkSlot", player),
	}

The function (They store string values) (They all use this same function):

local function updateEquippedSlots(newString, slot)
		
		if slot then
			print(slot.Value)
			print(slot.Name)
			print(newString)
			slot.Value = newString
			print("yes")
		end
end

Here is where the OnUpdate takes place. I’m not too sure why it doesn’t update when in-game whenever I try to change the value. (As you can see I try my best to shorten code as much as possible)

for key, stringValue in pairs(slotsStore) do
		updateEquippedSlots(stringValue:Get(defaultEquippedSlots[key]), player.Slots:FindFirstChild(key))
		stringValue:OnUpdate(updateEquippedSlots)
	end

Sorry for bumping this old topic, I just didn’t feel to make a new one since this one is extremely similar to mine. I just have something a little different. If anyone needs more code parts let me know!

1 Like