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

Workspace.SCP-075.SCP.Part.Script:4: attempt to index nil with 'Health' server script help?

Asked by 2 years ago

function onTouched(part) local h = part.Parent:findFirstChild("Humanoid")

    h.Health = h.Health -25
            wait(2)
    h.Health = h.Health -5

end

script.Parent.Touched:connect(onTouched)

0
Please stop spamming the question tab theking66hayday 841 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

It seems the parent of the part does not have an humanoid maybe? try this:

function onTouch(part)
    if part.Parent:FindFirstChild("Humanoid") then
        local h = part.Parent.Humanoid

        h.Health = h.Health - 25
        wait(2)
        h.Health = h.Health - 5
    else
        print(part.Parent.Name, "does not have an humanoid!")
    end
end

script.Parent.Touched:Connect(onTouch)

Keep in mind there is no cooldown between touches, so the Touched event will fire infinitely as long as long as the part is being touched, if you don't want that to happen, you can add a cooldown using debounce or something, like this:

local TimeBetweenTouches = 2

local debounce = false

function onTouch(part)
    if debounce == false then
        debounce = true

        if part.Parent:FindFirstChild("Humanoid") then
            local h = part.Parent.Humanoid

            h.Health = h.Health - 25
            wait(2)
            h.Health = h.Health - 5
        else
            print(part.Parent.Name, "does not have an humanoid!")
        end

        wait(TimeBetweenTouches)
        debounce = false
    end
end

script.Parent.Touched:Connect(onTouch)
Ad

Answer this question