'How to fix: attempt to index nil with

Sound = script.Parent.Sound
Image = script.Parent.ScreenGui
Ready = true
function onTouch(t)
    local a = t.Parent:FindFirstChild("Humanoid")
    if a ~= nil and Ready == true then
        Ready = false
        local b = Image:Clone()
        local player = game.Players:FindFirstChild(a.Parent.Name)
        b.Parent = player.PlayerGui
        script.Parent.Sound:Play()
        wait(2)
        b:remove()
        wait(1)
        Ready = true
    end
end
script.Parent.Touched:connect(onTouch())

Error: Workspace.Part.Script:5: attempt to index nil with 'Parent' - Server - Script:5



Solution 1:[1]

Ah very simple issue

Sound = script.Parent.Sound
Image = script.Parent.ScreenGui
Ready = true
function onTouch(t)
    local a = t.Parent:FindFirstChild("Humanoid")
    if a ~= nil and Ready == true then
        Ready = false
        local b = Image:Clone()
        local player = game.Players:FindFirstChild(a.Parent.Name)
        b.Parent = player.PlayerGui
        script.Parent.Sound:Play()
        wait(2)
        b:remove()
        wait(1)
        Ready = true
    end
end
script.Parent.Touched:Connect(onTouch)

Omit the braces on the last line, because you were invoking the function onTouch with no parameters, now Connect invokes it with the part parameter, simple fix. https://developer.roblox.com/en-us/api-reference/event/BasePart/Touched

Reply if you need any more help :)

-- Harvey

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harvey