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

having problems with changing skyboxes in lighing?

Asked by 2 years ago

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:

01local button = script.Parent
02local lighting = game:GetService("Lighting")
03 
04local 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"
View all 21 lines...

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:

01local button = script.Parent
02local sky_val = script.Parent.Value
03local lighting = game:GetService("Lighting")
04 
05local 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
14end
15 
16button.MouseButton1Click:connect(onButtonClick)

i tested the code before i did the rest for the other skyboxes but i got an error message saying that

11 isnt a valid member of lighting

and i dont understand why...

I would greately appreciate any help, sne_123456

2 answers

Log in to vote
1
Answered by
blowup999 659 Moderation Voter
2 years ago

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:

1local 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
8end
0
i found another way: look below but thanks! sne_123456 439 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

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!

01local button = script.Parent
02local sky_val = script.Parent.Value
03local lighting = game:GetService("Lighting")
04 
05local 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 
20button.MouseButton1Click:connect(onButtonClick)

Answer this question