I’m trying to have a part that when touched, sets the value of my frame.Visible to true. Sound simple enough right? well this code below is doing just that. Detecting the touch, finding the GUI element based on the character that collided with the part, and displaying the GUI. It works completely fine. But there’s a catch.
This part is in a subset of Folders of levels in my game. When a player teleports to the level it clones the level from repStorage into the workspace. That in turn contains this part and its script. All of the process works, you teleport, it loads in, the brick displays the menu that returns you to the lobby and :Destroys the instance of that level from the workspace and resets all necessary booleans to their normal values. But now, if you teleport back to the level, in the same way, doing the same clone function, the endPart no longer sets the GUI’s .Visible to true. To make matters worse, it even prints “touched” on the line just above! I change nothing about the situation, and the system no longer works. I came here as a last resort, I’ve tried moving the definitions of my variables around inside that script to ensure that the playerGUI i’m trying to access exists and since it works the first time, and I don’t modify the playerGUI folder afterwards, I have absolutely no idea why it can’t find it the second time around. Any ideas would be greatly, greatly appreciated!
Here’s a video I made showcasing the problem and going a little bit more in depth about the system surrounding the issue. Again, if anyone can help here I will seriously appreciate it:)
local endTP = script.Parent;
local db = false;
endTP.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local players = game:GetService("Players")
local character = hit.Parent
local player = players:GetPlayerFromCharacter(character);
local playerGUI = player:WaitForChild("PlayerGui");
local levelFinishedFrame = playerGUI.levelFinishedGUI.levelFinishedFrame;
if db == false then
db = true;
print("Touched")
levelFinishedFrame.Visible = true;
wait(1);
db = false;
end
end
end)
local touchPart = script.Parent
local debounce = false
touchPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if not debounce then
debounce = true
Player.PlayerGui:WaitForChild("levelFinishedGUI"):WaitForChild("levelFinishedFrame").Visible = true
wait(1)
debounce = false
end
end
end)
So I went and used that code in the script. Based on looking at it, it’s generally the same thing with some extra :WaitForChild()s to ensure the playerGui element is found properly. When I tested it, it worked fine the first time as I expected, but again on the second occasion it broke in the same way as before. I appreciate the clean code though, but this wasn’t quite the solution it seems. I’m interested to see where this goes because I feel like the way that both of us scripted the solution, make sense although it’s not working and no errors in the output as usual I suppose with the hardest of bugs haha
local endTP = script.Parent;
local db = false;
endTP.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local players = game:GetService("Players")
local character = hit.Parent
local player = players:GetPlayerFromCharacter(character);
local playerGUI = player:WaitForChild("PlayerGui");
local levelFinishedFrame = playerGUI.levelFinishedGUI.levelFinishedFrame;
print(db)
if db == false then
print(db)
db = true;
print(db)
print("Touched")
levelFinishedFrame.Visible = true;
wait(1);
db = false;
print(db)
end
end
end)
Oh shoot, I just noticed you edited the print script after I copied it in. Ill do a re test with all the prints included. And yeah it reallly shouldd work but it ain’t. These are the hardest problems to solve
Very much so, I understand if you can’t come up with anything because this is just sooo weird. I really can’t see anything obvious that would be causing it, the codes solid, the situation is the same, no error messages. A real pickle i’ve got myself into. Thanks for the help though LowkeyBoom, I really appreciate you taking some of your time to help me out! If you think of anything later on I’m all ears. Maybe someone will come along who had the same issue and knows how to fix it but for now I’d say it’s kind of just broken in a way I can’t see.
Hey!! I figured it out. Through a lot of trial and error. The way I initially fixed it was by printing the value of .Visible right before I try to set it to true. The first time, it printed false as you would expect. The second time however, it printed true when I couldn’t see it and before it was set to true it the script. Obviously something was wrong. The patch quick fix was to force it to be false right before but I knew that was janky and not best way to approach the solution. So I dig some more digging and realized that when I was teleporting the player back, I was setting .Visible to false on the client. And since the part looking for it is in the workspace, on the server. A client side change to visibility wouldn’t be noticed by a server side touched function on a part. So, I used a remote event to turn off the GUI when going back to the lobby from the server and then everything worked as planned.
TL;DR - Remote events! use them always, always always always. They prevent stuff from breaking your game:)
Thanks for all the help with this problem. It was a doozy.