Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

The script doesn't work nor GUI online, but works fine in Studio! What's wrong?

Asked by 6 years ago

So I'm trying to make a simple GUI in which when you click the button, another GUI will appear, and when you click it again, it disappears. It's work perfectly in Studio but whenever I publish it to Roblox, when I join it via Roblox website, it doesn't work and the error reads Rarities is not a valid member of ScreenGui I'm still new to scripting but I don't understand what's wrong. Here's my script.

~Note: It's Local

Button = script.Parent.TextButton
List = script.Parent.Rarities
Open = false

Button.MouseButton1Down:connect(function(open)
    if Open == false then
        Button.Text = "Close"
        List.Visible = true
        Open = true
    elseif Open == true then
        Button.Text = "Rarities"
        List.Visible = false
        Open = false
    end
end)
0
Add a WaitForChild for the variables thats why, In studio its all perfect but on client/real servers its not. UltraUnitMode 419 — 6y

1 answer

Log in to vote
3
Answered by
Newrown 307 Moderation Voter
6 years ago

This is caused because in studio, the player files load up much faster than in a real server. An easy fix for this would be to use WaitForChild. This pauses the thread until the specified variables exist.

So the code should look something like this:

Button = script.Parent:WaitForChild("TextButton")
List = script.Parent:WaitForChild("Rarities")
Open = false

Button.MouseButton1Down:connect(function(open)
    if Open == false then
        Button.Text = "Close"
        List.Visible = true
        Open = true
    elseif Open == true then
        Button.Text = "Rarities"
        List.Visible = false
        Open = false
    end
end)

0
I ended up solving it to. I added a "Wait (3)" on the first line. Since you seem like you know more I'll just do what you did. Also two questions. 1. What does "Child" stand for? Player? 2. Do I need to delete the question now? DjStarters 7 — 6y
0
WaitForChild is a built-in method which waits until the object in between the quotations exists. An object is a child of another(sub-class) if it is located under another object(class), in this case, the script is waiting for the children of the parent of the script called ("TextButton") and ("Rarities") to exist before it continues executing the code, this link could help you understand Newrown 307 — 6y
0
Also, don't delete the question. It may help someone else! Bellyrium 310 — 6y
Ad

Answer this question