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

How can I fix this problem?

Asked by
mine248 40
9 years ago

So, when I was making a game, I made my code, and tested it on ROBLOX Studio. It worked. But, in ROBLOX Player, it failed to work.

The code was:

local Background1 = script.Parent.Background1
local Background2 = script.Parent.Background2
local Background3 = script.Parent.Background3

script.Parent.Background1.Play.MouseButton1Click:connect(function()
    Background1:Destroy()
    Background2.Visible = true
end)

script.Parent.Background2.Normal.MouseButton1Click:connect(function()
    Background2:Destroy()
    Background3.Visible = true
end)

Are there any solutions?

1 answer

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
9 years ago

I believe the problem is that the code is running too quickly for the models to load into the game. You need to add WaitForChild(), or this line of code with each Background:

repeat wait until script.Parent.Background1
local Background1 = script.Parent.Background1
repeat wait until script.Parent.Background2
local Background2 = script.Parent.Background2
repeat wait until script.Parent.Background3
local Background3 = script.Parent.Background3

if you want to use WaitForChild(), then try this bit of code:

local Background1 = script.Parent:WaitForChild("Background1")
local Background2 = script.Parent:WaitForChild("Background2")
local Background3 = script.Parent:WaitForChild("Background3")

The WaitForChild()method is clearly more efficient, however both of these will still work, as they both pretty much do the same thing, and wait for the models to load in.


Anyways, I believe the code should work correctly now. If you have any further problems or questions, please leave a comment below. Hope I helped :P

0
This is a button function and there are no outsider variables, meaning this will never run until triggered. Therefore, there isnt a point for WFC here unmiss 337 — 9y
Ad

Answer this question