Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I fix this problem?

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

✓ Accepted answer

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

1 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.