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:

local button = script.Parent
local lighting = game:GetService("Lighting")

local function onButtonClick()
    if button.Text == "Sunset" then
        lighting.Sunset.Parent = script.Parent.skyboxes
        script.Parent.skyboxes.Mountain = lighting
        button.Text = "Mountains"
    if button.Text == "Mountains" then
        lighting.Mountain.Parent = script.Parent.skyboxes
        script.Parent.skyboxes.City.Parent = lighting
        button.Text = "City"
    if button.Text == "City" then
        lighting.City.Parent = script.Parent.skyboxes
        button.Text = "Sky"
    end
end
    end
    end

button.MouseButton1Click:connect(onButtonClick)

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:

local button = script.Parent
local sky_val = script.Parent.Value
local lighting = game:GetService("Lighting")

local function onButtonClick()
    if button.Text == "Sunset" then
        for i = 1,#lighting:GetChildren() do
            if lighting[i].ClassName == "Sky" then
                lighting[i].Parent = script.Parent.skyboxes
                script.Parent.skyboxes.Mountain.Parent = lighting
            end
        end
    end
end

button.MouseButton1Click:connect(onButtonClick)

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

1 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:

local function onButtonClick()
    for i,v in pairs(lighting:GetChildren()) do
        if v.Name == button.Text then
            script.Parent.skyboxes:ClearAllChildren()
            v:Clone().Parent = script.Parent.skyboxes
        end
    end
end
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!

local button = script.Parent
local sky_val = script.Parent.Value
local lighting = game:GetService("Lighting")

local function onButtonClick()
    if sky_val.Value == 1 then
        lighting:FindFirstChildWhichIsA("Sky").Parent = script.Parent.skyboxes
        script.Parent.skyboxes.Mountain.Parent = lighting
        sky_val.Value = 2
        button.Text = "Mountains"
        wait(1)
    elseif sky_val.Value == 2 then
        lighting:FindFirstChildWhichIsA("Sky").Parent = script.Parent.skyboxes
        script.Parent.skyboxes.City.Parent = lighting
        sky_val.Value = 3
        button.Text = "City"
        end
    end

button.MouseButton1Click:connect(onButtonClick)

Answer this question