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.
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)
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)
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)
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)