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

Why did I got "Attempt to index global 'x' a nil value" Error?

Asked by 6 years ago

This script sometimes works, but sometimes doesn't. It says that there are error in Line 22.

PlayerName = script.Parent.Parent.Parent.Name
Tool = script.Parent
Damage = script.Parent.Settings.FakeDamage
local ButtonPressed = false

Tool.Equipped:connect(function(Mouse)
    Mouse.Button1Down:connect(function()
----------------------------------------------------------------------  
        script.Parent.DamagePart.Touched:connect(function(hit)
            if not ButtonPressed then
                --is it not pressed?
                ButtonPressed = true
                print("Here")
                -----------------------
                if hit.Parent then
                local human = hit.Parent
                hum = human:FindFirstChild("Settings")
                if hum then
                    hum.HP.Value = hum.HP.Value - script.Parent.Settings.FakeDamage.Value

                    x = hit.Parent:FindFirstChild("Fighters")
                    y = x:FindFirstChild(PlayerName)
                    if not y then
                        z = Instance.new("IntValue")
                        z.Parent = x
                        z.Name = PlayerName
                    end
                else
                    print("Settings Not Found")
                end

                wait(1)
                end
                ------------------------
                ButtonPressed = false
            end
        end)
---------------------------------------------------------------------
    end)
end)

2 answers

Log in to vote
0
Answered by 6 years ago

hit.Parent does not have a child with this name

"Fighters"

that is why you are getting a nil value

I would recommend making sure that you are getting the child "Fighters" from the right parent

0
There are "Fighters" in the Monster Model, but still.. FlonexVorry 28 — 6y
Ad
Log in to vote
0
Answered by
522049 152
6 years ago

Try a minor change to line 18: if hum and hit.Parent:FindFirstChild("Fighters") then

Answer this question