Hi everyone,
So i was asked as a suggestion in my game to make a button that can be clicked to change skyboxes, however i tried many pieces of code but to no avail. My skyboxes are: sunset, mountains, City and sky.
Here is 1 piece of code that i used:
01 | local button = script.Parent |
02 | local lighting = game:GetService( "Lighting" ) |
03 |
04 | local function onButtonClick() |
05 | if button.Text = = "Sunset" then |
06 | lighting.Sunset.Parent = script.Parent.skyboxes |
07 | script.Parent.skyboxes.Mountain = lighting |
08 | button.Text = "Mountains" |
09 | if button.Text = = "Mountains" then |
10 | lighting.Mountain.Parent = script.Parent.skyboxes |
11 | script.Parent.skyboxes.City.Parent = lighting |
12 | button.Text = "City" |
13 | if button.Text = = "City" then |
14 | lighting.City.Parent = script.Parent.skyboxes |
15 | button.Text = "Sky" |
After investigation, i quickly realised that the code would only work the 1st time i pressed the button and after i pressed it the second time it said that sunset isnt a member of lighting, i hadnt realised that even if the if statement hadnt applied roblox would still go through it, i i found a way around it that still doesnt work.
New code:
01 | local button = script.Parent |
02 | local sky_val = script.Parent.Value |
03 | local lighting = game:GetService( "Lighting" ) |
04 |
05 | local function onButtonClick() |
06 | if button.Text = = "Sunset" then |
07 | for i = 1 ,#lighting:GetChildren() do |
08 | if lighting [ i ] .ClassName = = "Sky" then |
09 | lighting [ i ] .Parent = script.Parent.skyboxes |
10 | script.Parent.skyboxes.Mountain.Parent = lighting |
11 | end |
12 | end |
13 | end |
14 | end |
15 |
16 | button.MouseButton 1 Click:connect(onButtonClick) |
i tested the code before i did the rest for the other skyboxes but i got an error message saying that
1 | 1 isnt a valid member of lighting |
and i dont understand why...
I would greately appreciate any help, sne_123456
The problem here is you need to index lighting with the name of the skybox. You're indexing it like lighting[i] where i is 1, but the actual name of it is "Sunset" or "Mountain" I would change it to:
1 | local function onButtonClick() |
2 | for i,v in pairs (lighting:GetChildren()) do |
3 | if v.Name = = button.Text then |
4 | script.Parent.skyboxes:ClearAllChildren() |
5 | v:Clone().Parent = script.Parent.skyboxes |
6 | end |
7 | end |
8 | end |
Thanks a lot for helping everyone, i have already found a solution to this. Instead if using methods of identifying the skybox through the name, i tried to identify though its class name and for anyone else facing the same issue heres the code:
btw, i used a number value to identify which skybox is on, but you can also use the buttons name!
01 | local button = script.Parent |
02 | local sky_val = script.Parent.Value |
03 | local lighting = game:GetService( "Lighting" ) |
04 |
05 | local function onButtonClick() |
06 | if sky_val.Value = = 1 then |
07 | lighting:FindFirstChildWhichIsA( "Sky" ).Parent = script.Parent.skyboxes |
08 | script.Parent.skyboxes.Mountain.Parent = lighting |
09 | sky_val.Value = 2 |
10 | button.Text = "Mountains" |
11 | wait( 1 ) |
12 | elseif sky_val.Value = = 2 then |
13 | lighting:FindFirstChildWhichIsA( "Sky" ).Parent = script.Parent.skyboxes |
14 | script.Parent.skyboxes.City.Parent = lighting |
15 | sky_val.Value = 3 |
16 | button.Text = "City" |
17 | end |
18 | end |
19 |
20 | button.MouseButton 1 Click:connect(onButtonClick) |