Hello Devs! I was just wondering how I could make a Gui appear when touching a part. I thought this would be very helpful! Thanks. Please apply a script or explanation. Thanks.
Edit: 13/07/24 (this is very old and i had no idea how to script, I’m sure there are MUCH better threads to get info abt gui on touch from than this! You could probably find a script below and add a debounce feature to stop flickering)
All you have to do is make a ScreenGUI in the StarterGUI and add everything else and then after that you should add a part so that we know thats the part we are aiming to touch.
Next, create a script on the Part making sure the script is the parent of the part and once you have done that add this to the script.
local Part = game.Workspace.Part
local plr = game.Players.LocalPlayer
local PlayerGUi = game.Players.plr:FindFirstChild(“PlayerGUI”)
local ScreenGUI = game.Players.plr.PlayerGUi:FindFirstChild(“ScreenGUI”)
local Frame = game Playees.plr.PlayerGUI.ScreenGUI:WaitForChild(“Frame”)
Once you have called everything create a function so that we know if the player has touched the part then the GUI will appear.
script.Parent.Touched:Connect(function(hit)
if hit then
local player = hit.Parent
if player then
Frame.Visible = true
end
end
end)
Note: If theres any error here then let me know because I typed this by heart.
Create your ScreenUI, and create a frame or some UI element inside that.
Then create a localscript, inside this last UI element you created and paste this inside:
local part = workspace.Part;
local plr = game.Players.LocalPlayer;
local ui = script.Parent;
part.Touched:Connect(function(hit)
if plr.Character ~= nil and hit.Parent == plr.Character then
ui.Visible = true;
end
end)
That’s not false. I usually make myself a custom function for that using the method :GetTouchingParts(). However, I chose a simple way to explain this to a new player, and TouchEnded even if not reliable is still a thing
Yup exactly. ScreenGui’s are here only to hold other UI elements which include the Visible property. And yup, however LocalPlayer only works on the local side of the game, and meant to use in localscripts.
and for the ; well it’s useless in lua but it’s an habit I got from writing in other languages. ; is optional in js too, but mandatory in php for example.
As @MrLonely1221 said, and I agreed the Event TouchEnded is not reliable. It is meant to fire when the collision between two bricks (in this case you, and the part) ends.
However it fires way too often, as collisions are unstable between both.
Remove the part of the code including the function fired by the TouchEnded event, and I advise you create a close button in your UI element.
Or, more complicated you can make your own TouchEnded function but it’s more likely badly optimized.
Hey, sorry for being THAT late, but is there a way to make it so when a player touches a part, the GUI appears for everyone in the server and not just the player who touched it? I know a single bit about Lua U (i’m a beginner) and i don’t know how to make what i just said. Do you have any idea on how to do that?
local Part = game.Workspace.YourTouchPart
local Players = game.Players
function EveryoneUI()
for _,v in pairs(Players:GetPlayers()) do
v.PlayerGUI.ScreenGUI.Enabled = true
end
end
Part.Touched:Connect(function (Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
Print("Humanoid")
EveryoneUI()
end
end)