Hey guys! I have a quick question. I’m making a script so when you touch an egg, a GUI pops up, and you can close it by clicking on it, and then your client once again waits for you to touch the egg. Here’s my script:
function Constant()
local egg = script.Parent.egg:WaitForChild("egg")
local text = egg.Text
print(text)
local targetEgg = script.Parent:FindFirstChild(text)
local i = 1
targetEgg.Visible = true
targetEgg.TextButton.BackgroundTransparency = 1
targetEgg.BackgroundTransparency = 1
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
repeat
wait(0.02)
i = i - 0.01
targetEgg.TextButton.BackgroundTransparency = i
targetEgg.BackgroundTransparency = i
if i <= 0 then
i = 0
end
until i == 0
repeat
wait(1)
until targetEgg.Visible == false
egg.Parent:ClearAllChildren()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
Constant()
end
Constant()
As you can see, there is an infinite yield for waiting for the egg folder child to appear. Once it appears, the localscript grabs the text of the texlabel (which the server creates a textlabel child under the folder “egg” when it detects a player has touched the egg, and sets the text of the texlabel to the name of the Egg Gui it wants to activate) and then “activates” the Gui Frame with the name of that text. Is there a better way to do this?
Thanks for responding! Although that would physically get rid of the “infinite yield”, it would still basically be checking if an egg exists every time the script refreshes. I’m more concerned on whether or not this will have an impact on the game performance, or if it could cause any issues in general.
The issue here is that, this script needs to be able to initiate the GUI function any time. Whether you were in the game for 15 seconds, or 15 hours. With a timeout, once the timer is set to zero and it returns nil, you can no longer repeat the function. Would a RemoteEvent work in this case maybe? I’m new to those so I didn’t consider it.
I would detect every single hit the character goes through and check to see if the hit part is the egg. If it is the egg then toggle the gui. Add a stringvalue or something to the egg to check for or you can use CollectionService. CollectionService | Documentation - Roblox Creator Hub
Alternatively you can detect which players touch the egg on the server and then toggle the gui through a remote event.
Yeah, that was my thought. When the player touches the egg, a server script in the egg creates a text label with certain data in the “egg” folder of player gui, and the player is checking every refresh for if the egg child exists. Obviously this is a bad idea but it was what RemoteEvents were basically made for, so I’ll try it out.
The server is checking if the player touched the egg. If the player is touching an egg, the server will add a textlabel child to a folder in the PlayerGui.
The client is constantly waiting for this child to appear. When it does, the client will see what the text for the textlabel is, let’s say the text is ‘GlitchEgg’, then the client will display the frame with the name GlitchEgg, and when the player closes this frame and it isnt on display, all children in that one PlayerGui Folder get destroyed. The client will then repeatedly wait for another child to repeat the process.
But i don’t understand why you are only sending a textlabel over. Why not send a whole gui over and have it destroy itself when the player clicks close. Or have the client create the gui when it touches the egg.
You’d have to redefine what you want it to say, the position, the transparency, etc… every time in that case. The workaround to this is to put it in replicatedStorage and create a clone. But by that point, is it even still worth it? It would be much easier to just send over a child (by the way which I changed to a stringValue because why would I use a textlabel lol) and then from there display the GUI. I will try out the childAdded though as that would probably fix my infinite yield.