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

I keep getting this "attempt to index nil with 'PlayerGui'" error, any help?

I am making a part so when I touch it, it makes a LocalScript in the PlayerGui NOT disabled. But I always come up with the "attempt to index nil with 'PlayerGui' " error. Any help fixing this problem?

Error:
https://imgur.com/z4P8EaZ

Code:

local debounce = false

function onTouched(hit)
    if debounce == false then
        debounce = true
        game.Players.LocalPlayer.PlayerGui.Dialog4.Disabled = false
        debounce = false
    end
end

script.Parent.Touched:connect(onTouched)
-- Script is placed inside a Part, inside the Workspace.

1 comment

0

Couldn't you do whatever Dialog4 does in this script?

1 answer

✓ Accepted answer

You cannot use "LocalPlayer" unless it's a local script, if you want to get the player that touched that part, it is very simple.

The Touched event returns the part that touched that part, so we can check if the part that touched belongs to a player's character, get the player, and then we will be able to disable that script for them.

So the script should look something like this, instead of what you initially had.

local debounce = false

function onTouched(hit)
    if debounce == false and hit.Parent:FindFirstChild("Humanoid") then -- check if it is a player
        debounce = true
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent) -- returns the player
        Player.PlayerGui.Dialog4.Disabled = false -- set it it false
        debounce = false
    end
end

script.Parent.Touched:connect(onTouched)

That's it.

Hope I helped, if you have any questions please let me know.

1 comment

0

No questions and tysm

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