Can't Access Leaderboard!

For some reason, I’m trying to access the leaderboard and add money to it when a number is below 1, but it keeps saying, " Workspace.Script:6: attempt to index nil with ‘leaderstats’ "

This is the script:

local player = game.Players.LocalPlayer

while true do
	if workspace.Fly.FlyHealth.Value <= 1 then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
	end
end
1 Like

it seems you also are missing a wait statement, the script won’t work without it

local player = game.Players.LocalPlayer

while wait() do
if workspace.Fly.FlyHealth.Value <= 1 then
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
end
end

2 Likes

Well you are trying to do a client side local player from the server, instead try this, put the script in starter player scripts.

local plr = script.Parent

local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")

local Fly = workspace:WaitForChild("Fly"):WaitForChild("FlyHealth")

while true do
	if Fly.Value <= 1 then
		coins.Value += 1
	end
	task.wait(1) -- custom time here
end
1 Like

Your script didn’t do anything

You have to use a local script and you have to create the leaderstats

Server side script for leaderstats
local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = leaderstats
end

Players.PlayerAdded:Connect(leaderboardSetup)
Client side script for adding to the coins value
local player = game.Players.LocalPlayer
local fly = workspace.Fly

fly.FlyHealth.Changed:Connect(function()
	if fly.FlyHealth.Value > 1 then
		return
	end

    player.leaderstats.Coins.Value += 1
end)

Though the client side doesn’t replicate over to server, meaning other people can’t see it.
You have to somehow determine the players you want to give coins to in a server sided script and give the coins over there if you want it to replicate over to the server.

The problem with the old script is that the script try’s adding to something that hasn’t been loaded/found. An easy fix for this is to wait for the item to load and reference it at the top.

local player = game:GetService("Players").LocalPlayer--The player
local leaderstats = player:WaitForChild("leaderstats")--Waiting for the leaderstats to load
local coins = leaderstats:WaitForChild("Coins")--Waiting for the coins to load
local flyHealth = game:GetService("Workspace"):WaitForChild("Fly"):WaitForChild("FlyHealth")--Also waiting for the fly health to load aswel

while true do--a while loop to check if the fly health is less then or eqaul to 1 before adding 1 coin
	if flyHealth.Value <= 1 then--if the fly health is less then or eqaul to 1
		coins.Value += 1--add one coin
	end
	task.wait()--also adding a wait because if I don't the script will time out, you can use task.wait() if you need to add coins instantly or wait if you need to add coins quick but not instantly
end
1 Like

I would prefer the script to be in the workspace

One second I will convert it to a script

Here you go. Place the script in ServerScriptService.

local Players = game:GetService("Players")
local FlyHealth = workspace.Fly.FlyHealth

while task.wait(1) do
	if FlyHealth.Value > 1 then continue end
	for _, Player in Players:GetPlayers() do
		local leaderstats = Player:FindFirstChild("leaderstats")
		if not leaderstats then continue end
		local coins = leaderstats:FindFirstChild("Coins")
		if not coins then continue end
		coins.Value += 1
	end
end
2 Likes

This should work in workspace

game:GetService("Players").PlayerAdded:Connect(function(player)--checking when the player joins the game for the first time
	local leaderstats = player:WaitForChild("leaderstats")--Waiting for the leaderstats to load
	local coins = leaderstats:WaitForChild("Coins")--Waiting for the coins to load
	local flyHealth = game:GetService("Workspace"):WaitForChild("Fly"):WaitForChild("FlyHealth")--Also waiting for the fly health to load aswel

	while true do--a while loop to check if the fly health is less then or eqaul to 1 before adding 1 coin
		if flyHealth.Value <= 1 then--if the fly health is less then or eqaul to 1
			coins.Value += 1--add one coin
		end
		task.wait()--also adding a wait because if i dont the script will time out, you can use task.wait() if you need to add coins instantly or wait if you need to add coins quick but not instantly
	end
end)
2 Likes

Thank you so much for the Solution!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.