So I am trying to make a script where when somebody gets close to my door, I have a part called radius, and when they come in contact with it a GUI will pop up saying "Press {E} to interact"
Here is some extra information: -Interact is the name of the GUI -Radius is the name of the part they will need to touch
script.Parent.Radius.Touched:connect(function() game.StarterGui.Interact.Enabled = true end) script.Parent.Radius.TouchEnded:connect(function() game.StarterGui.Interact.Enabled = false end)
Hello! XMGQ, Try this
script.Parent.Radius.Touched:connect(function() game.StarterGui.Interact.Visible= true end) script.Parent.Radius.TouchEnded:connect(function() game.StarterGui.Interact.Visible= false end)
Make sure to have your GUI already set to visible as false! That should work :)
Edit:
So, in order to find the player who touched the brick we first got to make sure if what hit the brick is actually a player. To do this, we take advantage of an instance almost exclusive to players: the Humanoid. So to determine wether what got hit is a player or a random part, we just look for a humanoid: if one is found, it means a player hit the brick!
script.Parent.Radius.Touched:connect(function(PartHit) --"PartHit" is the part being hit if not PartHit.Parent:FindFirstChild("Humanoid") then return end
What got hit is a part, be it a random one or one belonging to the player (a leg, an arm, its torso or its head). So we will be looking for the Humanoid inside the player's model, not the part hit. That's why instead of calling FindFirstChild:() on the PartHit itself, we do it on its Parent (the player model in case what got hit was a player's body part). If no Humanoid was found, then we call return end which puts an end to the entire function.
As promised, here is the modified script from my original answer:
local Interact -- We left this variable undefined for now script.Parent.Radius.Touched:connect(function(PartHit) if not PartHit.Parent:FindFirstChild("Humanoid") then return end -- Check if what got hit is a player local Player = game.Players:GetPlayerFromCharacter(PartHit.Parent) Interact = Player.PlayerGui:FindFirstChild("Interact") -- Storing the GUI if Interact then Interact.Enabled = true -- Here we are accesing the GUI's copy belonging to the player who touched you Radius. end) script.Parent.Radius.TouchEnded:connect(function(PartHit) Interact.Enabled = false Interact = nil end)
Hope you find my explanaition usefull. Best regards!
The correct script is:
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players.LocalPlayer.PlayerGui.Interact.Frame.Visible = true end end) script.Parent.TouchEnded:connect(function(bye) if bye.Parent:FindFirstChild("Humanoid") then game.Players.LocalPlayer.PlayerGui.Interact.Frame.Visible = false end end)
After a while of playing around with my script, I came up with this. It seems to work perfectly fine for me. Thank you for your help, though!
Typed all of this on mobile so my code may look a little weird and might not work correctly as I can’t test it. Hoped this helped anyways though.
StarterGui does not constantly change GUI for players. It’s a place where all the GUI in it clones to players who join/respawn into something called PlayerGui.
If you’re using a server (regular) script, the function runs but you can’t change GUI in server scripts. Local scripts also won’t work as the .Touched function would then not run at all (from my experience).
You can use a server script, a local script, and a remote event to make this work. Remote events can communicate between client and server, which is just what you need.
So let’s say you have a part with a script in it, a remote event in ReplicatedStorage, and the local script and GUI in StarterGui (Or PlayerGui too). This is what you would do :
local RemoteEvent = game.ReplicatedStorage.RemoteEvent script.Parent.Touched:Connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then RemoteEvent:FireClient(player, "Touched") end end) script.Parent.TouchEnded:Connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then RemoteEvent:FireClient(player, "TouchEnded") end end)
local RemoteEvent = game.ReplicatedStorage.RemoteEvent RemoteEvent.OnClientEvent:Connect(function(data) if data == "Touched" then game.Players.LocalPlayer.PlayerGui.GUI.Enabled = true else game.Players.LocalPlayer.PlayerGui.GUI.Enabled = false end end)
So when something touches the part, the script checks if the part touched is from a player (lines 4-5). If it’s a player, the script will fire the RemoteEvent to the client or player that touched it, with a string that will tell the local script whether to enable the GUI or not. In the local script, when it receives the data, it’ll check if the data equals to "Touched" on line 4. If it does, it enables the GUI, otherwise it disables it.
Hopefully this helps you understand how to do this. Yes, you have to do all of this just to enable a GUI by touch, unless you have FilteringEnabled off, but it’s better to keep it on.
There are also probably a few things that you might need to read about to fully understand this, so I have provided some links below for you to read about. So...yeah. Hoped this helped.
https://wiki.roblox.com/index.php?title=Remote_Functions_%26_Events
https://wiki.roblox.com/index.php?title=API:Class/StarterGui
https://wiki.roblox.com/index.php?title=API:Class/ReplicatedStorage
http://wiki.roblox.com/index.php?title=API:Class/Players/GetPlayerFromCharacter
And sorry for making something a little simple this long. I just tried to explain how this works but I ended up oversimplifying it.