Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isn't my ranking leaderboard script working?

I want to make a rank system for my roblox game. It checks what the name of the local player is and puts something else in the leaderboard. This is the script:

local lplayer = game.Players.LocalPlayer
local leaderstats = Instance.new("StringValue")

leaderstats.Name = "Rank"
leaderstats.Parent = lplayer.leaderstats

while true
do
	if lplayer.Name == "blocky010101" then
		leaderstats.Value = "Owner"
	elseif lplayer.Name == "Clone3650" then
		leaderstats.Value = "Tester"
	else
		leaderstats.Value = "Player"
	end
end

But in output it comes up with this:

ServerScriptService.Script:5: attempt to index local 'lplayer' (a nil value)

why does this happen?

1 answer

✓ Accepted answer

Nooo... Players.LocalPlayer is nil on the server, only LocalScripts can access the local player. Instead, use a Players.PlayerAdded event, which can get the player, similar to the local player.

local names = {“blocky010101”, “Clone3650”}
local ranks = {“Owner”, “Tester”, “Player”}

game:GetService(“Players”).PlayerAdded:Connect(function(plr)
    local leaderboard = Instance.new(“Configuration”,plr)
    leaderboard.Name = “leaderstats”
    
    local rank = Instance.new(“StringValue”, leaderboard)
    rank.Name = “Rank”
    
    if plr.Name == names[1] then
        rank.Value = ranks[1]
    elseif plr.Name == names[2] then
        rank.Value = ranks[2]
    else
        rank.Value = ranks[3]
    end
end)

1 comment

0

Thankyou so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.