Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is my LeaderBoard script erroring?

Asked by 9 years ago

I have conducted multiple tests [In Online, and in Solo mode], but it keeps saying Workspace.LeaderBoard:17: attempt to index field 'Character' (a nil value), I have changed a few things in that part of the script, but it still keeps erroring, and I don't know what I'm supposed to do to fix it, because, as said before, I've conducted multiple tests to try and get it to work, here is the script;

local Admins = {"TheeDeathCaster","XAdminCommandsX","Kohltaphae","TheeFlameDoge","DeathWindZombie","Player1"}

print(#Admins.." users in list.")

local function ChkAdmin(plr,str)
for i = 1, #Admins do if plr:lower() == Admins[i]:lower() then return true end end
return false
end

local function onDeath(plr)
coroutine.wrap(function()
repeat wait() until plr
end)()
repeat wait() until plr:FindFirstChild("leaderstats") and #plr.leaderstats:GetChildren() > 0 and plr.Character and #plr.Character:GetChildren() > 0
repeat wait() until plr and plr.Character and plr.Character:FindFirstChild("Humanoid")
plr.Character.Humanoid.Changed:connect(function()
if plr.Character.Humanoid.Health <= 0 then
repeat wait() until plr.leaderstats:FindFirstChild("Deaths")
plr.leaderstats.Deaths.Value = plr.leaderstats.Deaths.Value + 1
end
end)
end

local function LeaderStats(plr)

local LeaderStat = Instance.new("IntValue",plr)
LeaderStat.Name = "leaderstats"
local Level = Instance.new("IntValue",LeaderStat)
Level.Name = "Level"
Level.Value = 0
local Deaths = Instance.new("IntValue",LeaderStat)
Deaths.Name = "Deaths"
Deaths.Value = 0
local Experience = Instance.new("IntValue",LeaderStat)
Experience.Name = "Exp"
Experience.Value = 0
local Money = Instance.new("IntValue",LeaderStat)
Money.Name = "Money"
Money.Value = 5

onDeath(plr)

if ChkAdmin(plr.Name,false) then
Level.Name = "Admin"
Level.Value = 1337
Money.Value = 9000
end

print(plr.Name.." has joined the server.")

end

game.Players.PlayerAdded:connect(LeaderStats)
for i,v in pairs(game.Players:GetPlayers()) do LeaderStats(v) end

1 answer

Log in to vote
1
Answered by
Merely 2122 Moderation Voter Community Moderator
9 years ago

On line 16 you call a function whenever any property of Humanoid is changed. If the Humanoid gets destroyed because it fell off the map or respawned, it's going to call that function. Inside that function, you did this:

if plr.Character.Humanoid.Health <= 0 then

You need to check to make sure Character exists and the Humanoid exists before checking the Health property. Here's how I recommend doing it.

local character = plr.Character or plr.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Changed:connect(function()
   if humanoid ~= nil and humanoid.Health <= 0 then
    repeat wait() until plr.leaderstats:FindFirstChild("Deaths")
    plr.leaderstats.Deaths.Value = plr.leaderstats.Deaths.Value + 1
end)
0
So, 'repeat until nil' loops aren't very good to use? :o TheeDeathCaster 2368 — 9y
0
They're not great. Merely 2122 — 9y
0
I have a question, how are 'repeat until nil' loops bad to use? I'm just a bit curious. :o TheeDeathCaster 2368 — 9y
0
Because you're running the code in the loop at least 30 times a second. Merely 2122 — 9y
Ad

Answer this question