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

1 comment
i found another way: look below but thanks!