Leaderstat value doesn't change

So I’ve had a problem with DataStores and GetAsync not working but then I tested with prints and it does work for some reason the value doesn’t get changed. Then I just did Coins = 100 and it didn’t change to a 100 and now I’m the most confused I’ve ever been. (yes this is a server script)

This is the part that doesn’t work

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")

local AUTOSAVE_INTERVAL = 60

local CoinsData
local StagesData
local LevelData
local RebirthsData
local AreasData
local ExpData
local MaxExpData

local function setupPlayerData(player)
	local playerUserId = player.UserId
	
	local Coins = player:WaitForChild("leaderstats").Coins.Value
	local Stages = player:WaitForChild("leaderstats").Stages.Value
	local Level = player:WaitForChild("leaderstats").Level.Value
	local Rebirths = player:WaitForChild("leaderstats").Rebirths.Value
	local Areas = player:WaitForChild("variables").Areas.Value
	local Exp = player:WaitForChild("variables").Exp.Value
	local MaxExp = player:WaitForChild("variables").MaxExp.Value
	Coins = 100 -- This doesn't change it too 100 while it should
	print(Coins)
	
	local success = pcall(function()
		CoinsData = playerData:GetAsync(playerUserId.."-coins")
		StagesData = playerData:GetAsync(playerUserId.."-stages")
		LevelData = playerData:GetAsync(playerUserId.."-level")
		RebirthsData = playerData:GetAsync(playerUserId.."-rebirths")
		AreasData = playerData:GetAsync(playerUserId.."-areas")
		ExpData = playerData:GetAsync(playerUserId.."-exp")
		MaxExpData = playerData:GetAsync(playerUserId.."-maxexp")
	end)
	if success then
		Coins = CoinsData
		Stages = StagesData
		Level = LevelData
		Rebirths = RebirthsData
		Areas = AreasData
		Exp = ExpData
		MaxExp = MaxExpData
	else
		warn("Cannot save data for player!")
	end
end

game.Players.PlayerAdded:Connect(setupPlayerData)

and here is the entire script

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")

local AUTOSAVE_INTERVAL = 60

local CoinsData
local StagesData
local LevelData
local RebirthsData
local AreasData
local ExpData
local MaxExpData

local function setupPlayerData(player)
	local playerUserId = player.UserId
	
	local Coins = player:WaitForChild("leaderstats").Coins.Value
	local Stages = player:WaitForChild("leaderstats").Stages.Value
	local Level = player:WaitForChild("leaderstats").Level.Value
	local Rebirths = player:WaitForChild("leaderstats").Rebirths.Value
	local Areas = player:WaitForChild("variables").Areas.Value
	local Exp = player:WaitForChild("variables").Exp.Value
	local MaxExp = player:WaitForChild("variables").MaxExp.Value
	Coins = 100 -- This doesn't change it too 100 while it should
	print(Coins)
	
	local success = pcall(function()
		CoinsData = playerData:GetAsync(playerUserId.."-coins")
		StagesData = playerData:GetAsync(playerUserId.."-stages")
		LevelData = playerData:GetAsync(playerUserId.."-level")
		RebirthsData = playerData:GetAsync(playerUserId.."-rebirths")
		AreasData = playerData:GetAsync(playerUserId.."-areas")
		ExpData = playerData:GetAsync(playerUserId.."-exp")
		MaxExpData = playerData:GetAsync(playerUserId.."-maxexp")
	end)
	if success then
		Coins = CoinsData
		Stages = StagesData
		Level = LevelData
		Rebirths = RebirthsData
		Areas = AreasData
		Exp = ExpData
		MaxExp = MaxExpData
	else
		warn("Cannot save data for player!")
	end
end

local function savePlayerData(playerUserId,player)
	local tries = 0
	local success
	
	local Coins = player.leaderstats.Coins.Value
	local Stages = player.leaderstats.Stages.Value
	local Level = player.leaderstats.Level.Value
	local Rebirths = player.leaderstats.Rebirths.Value
	local Areas = player.variables.Areas.Value
	local Exp = player.variables.Exp.Value
	local MaxExp = player.variables.MaxExp.Value
	repeat
		tries = tries + 1
		success = pcall(function()
			playerData:SetAsync(playerUserId.."-coins", Coins)
			playerData:SetAsync(playerUserId.."-stages", Stages)
			playerData:SetAsync(playerUserId.."-level", Level)
			playerData:SetAsync(playerUserId.."-rebirths", Rebirths)
			playerData:SetAsync(playerUserId.."-areas", Areas)
			playerData:SetAsync(playerUserId.."-exp", Exp)
			playerData:SetAsync(playerUserId.."-maxexp", MaxExp)
		end)
		if not success then wait(1) end
	until tries == 3 or success
	if not success then
		warn("Cannot save data for player!")
	end
end

local function saveOnExit(player)
	local playerUserId = player.UserId
	savePlayerData(playerUserId,player)
end

local function autoSave()
	while wait(AUTOSAVE_INTERVAL) do
		for _,player in pairs(game.Players:GetChildren()) do
			local playerUserId = player.UserId
			savePlayerData(playerUserId,player)
		end
	end
end
spawn(autoSave)

game.Players.PlayerAdded:Connect(setupPlayerData)
game.Players.PlayerRemoving:Connect(saveOnExit)

do not do like this

local Coins = player:WaitForChild("leaderstats").Coins.Value
Coins = 100

do like this

local Coins = player:WaitForChild("leaderstats").Coins
Coins.Value = 100
1 Like

Doesn’t work

local function setupPlayerData(player)
	local playerUserId = player.UserId
	
	local Coins = player:WaitForChild("leaderstats").Coins
	local Stages = player:WaitForChild("leaderstats").Stages
	local Level = player:WaitForChild("leaderstats").Level
	local Rebirths = player:WaitForChild("leaderstats").Rebirths
	local Areas = player:WaitForChild("variables").Areas
	local Exp = player:WaitForChild("variables").Exp
	local MaxExp = player:WaitForChild("variables").MaxExp
	Coins.Value = 100
	
	local success = pcall(function()
		CoinsData = playerData:GetAsync(playerUserId.."-coins")
		StagesData = playerData:GetAsync(playerUserId.."-stages")
		LevelData = playerData:GetAsync(playerUserId.."-level")
		RebirthsData = playerData:GetAsync(playerUserId.."-rebirths")
		AreasData = playerData:GetAsync(playerUserId.."-areas")
		ExpData = playerData:GetAsync(playerUserId.."-exp")
		MaxExpData = playerData:GetAsync(playerUserId.."-maxexp")
	end)
	if success then
		Coins.Value = CoinsData
		Stages.Value = StagesData
		Level.Value = LevelData
		Rebirths.Value = RebirthsData
		Areas.Value = AreasData
		Exp.Value = ExpData
		MaxExp.Value = MaxExpData
	else
		warn("Cannot save data for player!")
	end
end

game.Players.PlayerAdded:Connect(setupPlayerData)

is there a output error? 3.0 character

I already fixed it. It was because the GetAsync was overwriting the value change. and now I got the datastore working too finally. Datastores as such a pain

2 Likes