I am trying to link a leaderstat to a data store value but for some reason this function wont run when the integer for the leaderstat is changed. (.Changed:). I know the gold variable is defined correctly because the line before it runs perfectly fine. Any help?
local function LoadData(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Parent = leaderstats
local Success = nil
local playerData = nil
local attempt = 1
repeat Success, playerData = pcall(function()
return Database:GetAsync(player.UserId)
end)
attempt += 1
if not Success then
warn(playerData)
task.wait(3)
end
until Success == true or attempt >= 5
if Success then
print("Data!!!!!", playerData)
if not playerData then
print("Creating new data...")
playerData = {
["Gold"] = 10
}
end
SessionData[player.UserId] = playerData
else
warn("Unable to get data")
player:Kick("Data loading issue. Try again later.")
end
gold.Value = SessionData[player.UserId].Gold --- THIS WORKS CONFIRMED
gold.Changed:Connect(function() --- this does NOT fire when the int is changed
print("CHANGED")
SessionData[player.UserId].Gold = gold.Value
end)
leaderstats.Parent = player
end
Same thing, doesnt fire. I tried checking to see if .changed works in a local script if it is defined differently like this
local Gold = game.Players.LocalPlayer:FindFirstChild("leaderstats").Gold
Gold.Changed:Connect(function()
print("Changed elsewhere")
end)
And it DOES print this out. However, when I tried to define the gold value in the same way on the server script (player value is passed through) it didnt work.
Could the function being inside of the PlayerAdded() function cause it to not work? I was watching a video to better understand datastores and it worked fine for them but im not sure what else would cause it.
Ah, I did some testing and I see where you went wrong. This is because you’re not changing it for the server, you’re changing it on the client. Changes on the client only show for you, and not the server. Next to the stop button in studio, it’ll say “current client”, you can click that and change to the server’s view and make changes there that will replicate, to go back hit the button again.