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)