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

It's giving me this error. "Attempt to index a nil value" Line 7?

So, basically I'm having trouble trying to get the reward to be given to the player.

local human = script.Parent:WaitForChild("Humanoid")


human.Died:Connect(function()
    local tag = human:FindFirstChild("creator")
    if tag ~= nil then
	local stats = game:GetService("Players"):FindFirstChild(tag.Value).leaderstats
        if stats then
			print("heres ur money")
			Gold.Value = Gold.Value+script.Parent.Settings.Gold.Value
        end
    end
end)

3 comments

0

that indentation at the second end of the touched event....

0

use FindFirstChild() or WaitForChild() with a timeout instead of indexing leaderstats

0

Nothing worked, it still says "Attempt to index a nil value"

1 answer

Try this:

 script.Parent:WaitForChild("Humanoid").Died:Connect(function()
    local tag = human:FindFirstChild("creator")
    if tag ~= nil then
    local stats = game:GetService("Players").leaderstats:WaitForChild(tag.Value)
        if stats then
            print("heres ur money")
            Gold.Value = Gold.Value + script.Parent.Settings.Gold.Value
        end
    end
end)

1 comment

0

Doesn't work, and btw line 4 is incorrect.

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