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

How do I fix my gui only appearing once on OnTouched?

Ok so excuse me if this is a dumb question.

Im new to lua and I want to make a ontouch function which basically shows the gui when you touch it.

I did the following code:

script.Parent.Touched:connect(function(hit)
  	game.Players[hit.Parent.Name].PlayerGui.SellGUI.Main.Visible = true
end)

In a regular script.

When it shows the gui and I click the close button, then walk off the part and go back on, it doesnt show. All the close button does is make SellGUI.Main.Visible to false. I dont understand why this isnt working?

I literally cannot understand why this isnt working, and probably someone who knows lua can easily tell what the mistake probably is, but im trying to self learn.

So my question is, what did I do wrong? Is there a reason why this isnt working? Why does this gui only appear once?

Im new to this site and lua, so excuse any mistakes I did on this question or code.

Thanks to anyone who helps.

1 comment

0

It's important to know that when dealing with touched events you'll need a debounce. Otherwise your script will run multiple times and produce unwanted results.

2 answers

✓ Accepted answer

First thing is that you can't access "PlayerGui" with a normal script so we are going to make use of remote events. You would have this(normal script) inside that part:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenGui = ReplicatedStorage:WaitForChild("OpenGui")

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

After that create "RemoteEvent" and place it inside "ReplicatedStorage" and call it OpenGui

then create "localscript" inside "PlayerGui" and use this code inside:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenGui = ReplicatedStorage:WaitForChild("OpenGui")
local Player = game.Players.LocalPlayer

OpenGui.OnClientEvent:Connect(function()
	Player.PlayerGui.SellGUI.Main.Visible = true
end)

If you have any questions feel free to ask if this helped you make sure to mark it as a solution.

1 comment

0

Thank you, this works for me!

This line is wrong:

game.Players[hit.Parent.Name].PlayerGui.SellGUI.Main.Visible = true

You're doing game.Players then a string value. You need an object value there. Change that line to this:

game.Players:FindFirstChild(hit.Parent.Name).PlayerGui.SellGUI.Main.Visible = true

That will find the first object with the name of whatever you put in the parenthesis.

1 comment

0

It still doesnt fix the problem im having, when I step on it the first time, the gui pops up. When I step off and go back on, it doesnt show and it errors " 08:54:17.940 - Workspace.InvisibleTouchScript.Script:2: attempt to index a nil value", line 2 of the script is the code you put above.

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