Hello, i am trying to make a GUI appear when a part is touched and disappear when the part is not touched anymore. i have my part named “TouchPart” in the workspace, and i have ScreenGui with a Child Frame in the StarterGui. the script is inserted into the ScreenGui. when i run the game and touch the part nothing happens. i been stuck for hours trying to solve it. thanks for the support.
local touchPart = game.Workspace.TouchPart
local screenGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
local frame = screenGui:WaitForChild("Frame")
local function onTouchEnded(other)
local function onTouched(other)
local character = other.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
frame.Visible = true
end
end
touchPart.Touched:Connect(onTouched)
touchPart.TouchEnded:Connect(onTouchEnded)
local character = other.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
frame.Visible = false
end
end
Personally, id use GetPartsInPart or just use a module such as zones to do this for me. However if you wanted to do it with touched events then you are going to need a RemoteEvent to fire from the server to the clients who touched the part, and the script could look something like this:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local character = nil
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
character = hit.Parent
elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
character = hit.Parent.Parent
end
ReplicatedStorage.RemoteEvent:FireClient(character:GetPlayerFromCharacter(), true)
end)
script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
character = hit.Parent
elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
character = hit.Parent.Parent
end
ReplicatedStorage.RemoteEvent:FireClient(character:GetPlayerFromCharacter(), false)
end)
Note: I did not test this script at all, so there are bound to be errors. Take it as more of a guide than a copy paste. Good luck!