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?
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