Attempting to make an image appear on touch

Forgive my idiocy here, as I am a new scripter. I want to make the frame and the image label visible when you touch the part. I’m fairly new to scripting.
The code below gives me this error when I click play. It makes no sense to me as I’ve already defined part as the parent of the script.
image

function CheckTouched()
local part = script.Parent
local frameInGui = game.StarterGui.ScreenGui.Frame
local imageInGui = game.StarterGui.ScreenGui.ImageLabel
local plr = game.Players.LocalPlayer
local plrChar = plr.Character
local humanoid = plrChar:FindFirstChild("Humanoid")
	if humanoid then
		frameInGui.Visible = true
		imageInGui.Visible = true
	end
end
part.Touched:Connect(CheckTouched)

Are you sure that the part is the parent of the script?

You are setting the part inside of function. Try to set the local part outside of that function. The code will be looking like this:

local part = script.Parent
function CheckTouched()
    local frameInGui = game.StarterGui.ScreenGui.Frame
    local imageInGui = game.StarterGui.ScreenGui.ImageLabel
    local plr = game.Players.LocalPlayer
    local plrChar = plr.Character
    local humanoid = plrChar:FindFirstChild("Humanoid")
	if humanoid then 
    	frameInGui.Visible = true
    	imageInGui.Visible = true
  	end
end
part.Touched:Connect(CheckTouched)
1 Like

Still makes 0 sense to me haha! Thanks!

That code still won’t work properly. They’re editing it in StarterGui not PlayerGui; which means the Player will not see the change.

@LuaIsGarb it should look like:

local part = script.Parent
function CheckTouched(hit)
    if not game.Players:GetPlayerFromCharacter(hit.Parent) then return end -- if it isn't a player do nothing
    local plr = game.Players.LocalPlayer
    if game.Players:GetPlayerFromCharacter(hit.Parent) ~= plr then return end -- if the player isn't the local player do nothing
    local frameInGui = plr.PlayerGui.ScreenGui.Frame
    local imageInGui = plr.PlayerGui .ScreenGui.ImageLabel
    local plrChar = plr.Character
    local humanoid = plrChar:FindFirstChild("Humanoid")
    if humanoid then 
        frameInGui.Visible = true
    	imageInGui.Visible = true
    end
end
part.Touched:Connect(CheckTouched)

This should work.

1 Like

Don’t forget to mark my replay as a sloution, if it solved your problem.
It will keep forum more clean. Thanks!

Yeah, using @MegaBorecYT’s code gave me this. image

The error is unrelated to their code; it’s because you’re attempting to use LocalPlayer in a server-sided script.

Place the script in StarterPlayerScripts and it should be like:

local part = workspace.PathToPart
function CheckTouched(hit)
    if not game.Players:GetPlayerFromCharacter(hit.Parent) then return end -- if it isn't a player do nothing
    local plr = game.Players.LocalPlayer
    if game.Players:GetPlayerFromCharacter(hit.Parent) ~= plr then return end -- if the player isn't the local player do nothing
    local frameInGui = plr.PlayerGui.ScreenGui.Frame
    local imageInGui = plr.PlayerGui .ScreenGui.ImageLabel
    local plrChar = plr.Character
    local humanoid = plrChar:FindFirstChild("Humanoid")
    if humanoid then 
        frameInGui.Visible = true
    	imageInGui.Visible = true
    end
end
part.Touched:Connect(CheckTouched)