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.
1 comment
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?