Hey Robloxians,
I made a script which opens a gui when part is touched. However the script breaks if the part is touched once. The GUI do open but the script doesn’t seem work at all after the part is touched. There are no errors in the output. The script is really basic and I don’t see any errors in it, can someone help?
SCRIPT:
local brick = game.workspace.OpenGui
brick.Touched:Connect(Function(hit)
if hit and hit.Parent then
script.parent.Visible = true
end)
You have not finished the if statement at line 4.
there should be another end.
Also Function on line 4 should start with an uppercase letter, it should be function.
oh i have fixed it but it still works only one time and then breaks when the part is touched again:
local brick = game.Workspace.OpenGUI
brick.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") then
script.Parent.Visible = true
end
end)
What do you mean by “breaks”? Did you put a print statement in your Touched event and see if it is indeed still calling that event and it isn’t? Is there somewhere else in your code where you are setting the script.Parent.Visible = false? If not nothing would ever change as it would always be visible from that point on.
I have tried tons of script modification from basic to a little advanced and saw many tutorials but still the gui becomes visible for the time the part is touched once, and then there is no error in output, i didn’t write at break or return in script but still the script stops working something like its disabled
This implies to me the issue is with whatever code makes it disappear. Seeming that the close function either makes another part invisible that this doesn’t handle, or just destroys the gui.
I would recommend you to change the UI visibility on a local script rather then a server script.
You could do all of this through a Remote Event.
Example
Server Script:
local brick = game.Workspace.OpenGUI
local RemoteEvent = game.ReplicatedStorage.RemoteEvent --place your remote event in ReplicatedStorage and reference it here
brick.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") then
RemoteEvent:FireClient()
end
end)
Local Script:
local localPlayer = game.Players.LocalPlayer
local PlayerGui = localPlayer.PlayerGui
local OpenGui = PlayerGui.OpenGui --reference your gui here
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
RemoteEvent.OnClientEvent:Connect(function()
OpenGui.Visible = true
end)
No, the part and gui both are there, its just something with the script. I even tried something like this:
script.Parent.touched:Connect(function(hit)
Local plr = game.Players.GetPlayerFromCharacter(hit.parent)
If plr then
plr.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Frame").Visible = true
end
end)
Can you include the snippet of code that handles closing your gui? As far as I can tell, the opening part should work. So the only potential issue I can see is that closing it is changing a state that the opening script isn’t handling correctly.
I agree this is why I was asking where the Visible was set to false. Sometimes people move the Gui to slide it off the screen or set the background transparent instead which would make the Visible useless after the first time.
script.Parent.MouseButton1Click:Connect(function()
local frame = script.Parent.Parent
if frame.Visible == true then
frame.Visible = false
else
frame.Visible = false
end
end)
Hmm… Yeah I’m not sure, the last thing I can think of is maybe script.Parent.Parent of the close script is not equal to script.Parent of the open script. Though I don’t think that’s particularly likely.
Can you show a picture of your Explorer window with your Gui layout(if it differs that is)? I replicated what I assume is your layout, StarterGui, ScreenGui under StarterGui, Frame under ScreenGui along with the LocalScript for the part Touched event, Button under Frame with LocalScript to make invisible and it works with no problems. I did change the close button a bit because there is no need to have an IF statement and put the same code in both the true and false case so it was just:
script.Parent.MouseButton1Click:Connect(function()
local frame = script.Parent.Parent
frame.Visible = false
end)
Here is my setup:
If you do have this exact layout all I can suggest is close and restart Roblox Studio.