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

19: attempt to index a nil value?

Asked by 5 years ago
Edited 5 years ago

I'm trying to make a killstreak system that if your killstreak is 5 or more you get a crown and if it's less than 5 you lose the crown. But the problem is that the RemoveCrown function is giving me an error saying like I said in the title:

19: attempt to index a nil value.

Script:

-- Functions

function AddCrown(Player)
    repeat wait() until Player.Character

    local Char = Player.Character
    local Head = Char.Head

    local Crown = game.ServerStorage.CrownPart:Clone()
    Crown.Parent = Head

    while true do
        wait(0.01)
        Crown.CFrame = CFrame.new(Head.Position + Vector3.new(0, 3, 0))
    end
end

function RemoveCrown(Player)
    Player.Character.Head:FindFirstChild("CrownPart"):Destroy()
end

-- Runs functions

game.Players.PlayerAdded:connect(function(Player)
    repeat wait() until Player.Character

    local Killstreak = Player:WaitForChild("PlayerValues").Killstreak
    local Char = Player.Character

    while true do
        wait(0.1)

        if Killstreak.Value >= 5 then
            AddCrown(Player)
        else
            RemoveCrown(Player)
        end
    end
end)

I would really appreciate it if you help me with my problem, Plus I'll accept your answer.

Thanks, MajinBluee

0
It might be that the crown doesn't exist marijus06 68 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

You're not checking if the head actually contains CrownPart. And also, you don't need a loop to check their killstreak.

Fixed script:

-- Functions

function AddCrown(Player)
    repeat wait() until Player.Character

    local Char = Player.Character
    local Head = Char.Head

    local Crown = game.ServerStorage.CrownPart:Clone()
    Crown.Parent = Head
    spawn(function()
    while true do
    if not Crown then -- we don't want errors for every ten milliseconds
        break 
    end
        wait(0.01)
        Crown.CFrame = CFrame.new(Head.Position + Vector3.new(0, 3, 0))
    end
    end)
end

function RemoveCrown(Player)
    local crown  = Player.Character.Head:FindFirstChild("CrownPart")
    if crown then 
    crown:Destroy()
    end
end

-- Runs functions

game.Players.PlayerAdded:connect(function(Player)
    repeat wait() until Player.Character

    local Killstreak = Player:WaitForChild("PlayerValues"):WaitForChild("Killstreak") 
    local Char = Player.Character

    Killstreak.Changed:Connect(function()


        if Killstreak.Value >= 5 then
            AddCrown(Player)
        else
            RemoveCrown(Player)
        end
    end)
end)

Everything I have changed:

Added an extra WaitForChild at line 27 just in case

Listen for Killstreak.Changed instead of a loop(line 30)

Put the while true do (line 12) in spawn(function() so that the script can run while the loop is running.

Checked if Crown didn't exist inside the while true do at line 12. If it didn't, the script would stop the loop by using break.

Instead of Player.Character.Head:FindFirstChild("CrownPart"):Destroy()(line 19), I checked if the CrownPart existed, and if it did, the script would destroy it.

0
Thanks bro! MajinBluee 80 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Line 19 errors because you didn’t check if the child exists. Yes you used FindFirstChild, but no if statements to check its existence. There are some people who add ~= nil to check the existence of an object, but is redundant and inefficient.

local crown = Player.Character:FindFirstChild"Crown"

if crown then
    crown:Destroy()
end

I put the crown as an accessory inside of the character model.

Log in to vote
0
Answered by 5 years ago

Hello! You need to fix RemoveCrown function!

function RemoveCrown(Player)
if Player.Character.Head:FindFirstChild("CrownPart") then
Player.Character.Head:FindFirstChild("CrownPart"):Destroy()
end
end

If it helped you, don't forget to accpet the answer!

Marijus

0
I don’t think OP will accept an answer from a scripter who doesn’t even indent their code. User#19524 175 — 5y

Answer this question