Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why won't my GUI pop up when the radius is touched?

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)

4 answers

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

1 comment

0

Hmm. Didn't work. I don't understand why it wouldn't work, especially with enabled. Visible wasn't an option for my GUI, however it is available for my frame and text label. I also noticed i had done it in a normal script, so I transferred it over to a local script. Any ideas?

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!

3 comments

0

Still a little confused. This door is really being stubborn. Maybe explain to me how my script can find the player that touched the part so I can then further access the player's gui?

0

Sure, will edit my answer to address that topic. I'll leave the script there and modify the text part.

0

Nevermind, thank you for your help, though!

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!

2 comments

0

Hmmm, the way you are accesing the PlayerGui seems odd. It will work for roblox studio mode, but I'm almost sure this script won't work on the live game. I could be wrong of course, but I suggest you to try it on an actual roblox server.

0

You're right, it doesn't work in live games. I haven't been trying these scripts in live games, so that may be part of the problem. I will try again.

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.

##Problems

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

##Solution

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 :

###Script

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 Script

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.

###Links

And sorry for making something a little simple this long. I just tried to explain how this works but I ended up oversimplifying it.

This discussion is preserved as it was. New answers, comments, and votes are closed.