Why does my intValue duplicate?!

local Players = game:GetService("Players")

local replicatedStorage = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
task.wait()

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("TokenSS")

local Stat = "Tokens"
local startNum = 0

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		local leaderstats = player:WaitForChild("leaderstats")
		
		local Tokens = Instance.new("IntValue",leaderstats)
		Tokens.Name = Stat
		Tokens.Value = DS:GetAsync(string.format("tokens_%d", player.UserId)) or startNum
	end)	
end)

Players.PlayerRemoving:Connect(function(player)
	local PlayerLS = player:WaitForChild("leaderstats")
	local Tokens = PlayerLS:WaitForChild("Tokens")
	DS:UpdateAsync(player.UserId, Tokens.Value)
end)

Does it duplicate it? Or am I really stupid.

1 Like
local Players = game:GetService("Players")

local replicatedStorage = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
task.wait()

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("TokenSS")

local Stat = "Tokens"
local startNum = 0

Players.PlayerAdded:Connect(function(player)	
      local leaderstats = player:WaitForChild("leaderstats")
      local Tokens = Instance.new("IntValue",leaderstats)
      Tokens.Name = Stat
      Tokens.Value = 
      DS:GetAsync(string.format("tokens_%d", player.UserId)) or startNum	
end)

Players.PlayerRemoving:Connect(function(player)
	local PlayerLS = player:WaitForChild("leaderstats")
	local Tokens = PlayerLS:WaitForChild("Tokens")
	DS:UpdateAsync(player.UserId, Tokens.Value)
end)

Remove the character added event it is unnecessary, you just need to check if a player joins by PlayerAdded event. Also make sure leaderstats folder is actually inside the player.

2 Likes