What Wrong With This Code?

Can Anyone Solved this code ?

local Players = game:GetService("Players")
 
local function onCharacterAdded(character, player)
	player:SetAttribute("IsAlive", true)
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.Died:Connect(function()
		local points = player.leaderstats.Points
		points.Value = 0
		player:SetAttribute("IsAlive", false)
	end)
end
 
local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
 
	player:SetAttribute("IsAlive", false)
 
	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)
 
while true do
	wait(1)
	local playerList = Players:GetPlayers()
	for i = 1, playerList  do
		local player = playerList[i]
		if player:GetAttribute("IsAlive") then
			local points = player.leaderstats.Points
			points.Value = points.Value + 1
		end
	end
end

Im Trying To Make LeaderStats But it Wont
i think the roblox error maybe?

Can you please share a screenshot of the error you got; this will help me determine the cause of your script not working.

Instead do doing what u did at the end. Do this:

while true do
	wait(1)
	local playerList = Players:GetPlayers()
for i,v in pairs(playerList) do
		local player = v
		if player:GetAttribute("IsAlive") then
			local points = player.leaderstats.Points
			points.Value = points.Value + 1
		end
	end
end

There Is No Error But the leaderstats wont add the point

thx its work now! thx for your solution

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

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
	
	player:SetAttribute("IsAlive", false)

	player.CharacterAdded:Connect(function(character)
		player:SetAttribute("IsAlive", true)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			local points = player:WaitForChild("leaderstats"):WaitForChild("Points")
			points.Value = 0
			player:SetAttribute("IsAlive", false)
		end)
	end)
end)

while task.wait(1) do
	for _, Player in pairs(Players:GetPlayers()) do
		if Player:GetAttribute("IsAlive") then
			local points = Player:WaitForChild("leaderstats"):WaitForChild("Points")
			points.Value += 1
		end
	end
end

This is how I’d do things, for optimisation purposes. I’ve simplified the points giving loop down, and I’ve merged the events into a single block of code.