Problem with opening GUI from Event Touched

Hi my names are Pixeluted I want to do if player touch to part then GUI open but first time open second time does not open I don’t know why I using Script

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:FindFirstChild(hit.Parent.Name)
		Player.PlayerGui.ShopGui.Enabled = true
	end
end)

Thank for Read

3 Likes

I recommend using

game.Player:GetPlayerFromCharacter(hit.Parent) 

rather than

game.Players:FindFirstChild(hit.Parent.Name)

Here’s a quick note: you should handle all GUI related things on the client. So instead of using those lines, do:

local Part = -- the part that will open the GUI
local GUI = -- the GUI that will open

Part.Touched:Connect(function()
    if hit.Parent:FindFirstChild("Humanoid") then
        GUI.Enabled = true
    end
end)

May you please send us a video of the problem? I don’t see any problem with the script and the question is very vague.

That part of the script is the part that dosen’t work?
Can you show the other scripts?

i think i know your problem. you will need to use a remote event to make this work do
this

Server Script
–part thats being touched

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		game.ReplicatedStorage.EVENTNAMEHERE:FireClient(Player)
	end
end)

local script

game.ReplicatedStorage.EVENTNAMEHERE.OnServerEvent:Connect(function()

script.Parent.Enabled = true

end)

and then put a remove event in ReplicatedStorage and change the EVENTNAMEHERE to the remote events name

1 Like

put local script in gui thats why its script.Parent.Enabled = true

When I change around guis with a server script, it still works for me. I think it doesn’t matter whether it changes on the server, or changes on the client.

Enabled works, I don’t think visible is even a property of screen gui but idk

its better to use remote events to prevent hackers and a faster way to send info

1 Like

Yeah, but it’s probably not where the error came from.

1 Like

@C0lvy123 Thank you, I just noticed, will fix

@Lp_MewFX Remote events are the reason for hacking, not prevent . In addition, there is no info to be sent, and it won’t be “faster”. Sorry if I sounded rude.

1 Like

Wait, can something hacked if it is client or server side?

putting local behind a variable doesn’t make something on the client

There are 2 solutions to this problem, but I will show you the easiest one. I recommend adding a LocalScript inside of StarterPlayer -> StarterCharacterScripts and this code:

workspace.YourPartNameHere.Touched:Connect(function(hit)
	if hit.Parent.Name == script.Parent.Name then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		Player.PlayerGui.ShopGui.Enabled = true
	end
end)

Change “YourPartNameHere” to the Part’s name.

(note: I have not tested this code, so if there are any issues please tell me.)

1 Like

Try this:

script.Parent.Touched:Connect(function(Touched)
local Player = game.Players:GetPlayerFromCharacter(Touched.Parent)
if Player then
Player.PlayerGui:WaitForChild(“ShopGui”).Enabled = true
end
end)

Let me know if it worked :slight_smile:

You should never use a script when interacting with the Client, always use the Local Script when interacting with the client. :slight_smile:

When you do it from the server, the server will recognise Enabled as true. When the client hides the Gui and sets Enabled to false, the server will not see this. Therefore, when it attempts to set Enabled again, the server believes it’s already enabled and won’t do anything.

You should be doing this work from the client-side.

2 Likes

That’s literally his exact code

It is not really. I’m using it from a LocalScript, he’s using it from a Server script. It’s not recommended to enable/disable UIs from the server because then if the client disables it, the server can’t see that it disabled it and when it goes to enable it again it won’t work because it thinks it’s enabled, just as @colbert2677 said.

2 Likes

Thank But I need test it wait please

Really Good Reply I need all try