Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

[SOLVED] How do I make a toggleable button that makes parts appear and disappear?

Asked by 8 days ago
Edited 7 days ago

Information:

  • I am not getting any errors
  • I already attempted this, it didn't work.
  • The parent of the script game.StarterGui.GraphicsGui.GraphicsButton
  • The script is a LocalScript
  • When I click the button nothing happens.
  • The folder :https://ibb.co/Kbc14Wj

Attempt:

local qualityhigh = true
function Click(mouse)
    if qualityhigh == true then
        script.Parent.Text = "Increase Graphics"
        game.Workspace.HighQuality:Destroy()
        qualityhigh = false
    end
    if qualityhigh == false then
        script.Parent.Text = "Decrease Graphics"
        local copy = game.Lighting.HighQuality:Clone()
        copy.Parent = game.Workspace
        qualityhigh = true
    end
end


script.Parent.MouseButton1Down:connect(Click)

Edit: I fixed it by changing the parent and using elseif:

Answer:

local qualityhigh = true
function Click(mouse)
    if qualityhigh == true then
        script.Parent.Text = "Increase Graphics"
        game.Workspace.HighQuality.Parent = game.Lighting
        qualityhigh = false
    elseif qualityhigh == false then
        script.Parent.Text = "Decrease Graphics"
        game.Lighting.HighQuality.Parent = game.Workspace
        qualityhigh = true
    end
end


script.Parent.MouseButton1Down:connect(Click)
0
Do you want this on the client or on the server? Dovydas1118 1072 — 7d
0
@Dovydas1118 Client Dave_Robertson 20 — 7d

1 answer

Log in to vote
0
Answered by 7 days ago

Right, there is nothing much wrong with your script. If you want to destroy the clone, you're gonna need to name it. Naming your parts and using Instance:FindFirstChild() will be the key to destroying and cloning parts. Also, if you're using a function for only purpose, you might as well use an anonymous function to save yourself from naming things.

local qualityhigh = false --You should for the lower PC counterparts first.

script.Parent.MouseButton1Down:Connect(function() --Capitalise the connect, as it is deprecated. Also, if you're not using the parameter, don't use it.
    if not qualityhigh then --Same thing as if qualityhigh == false
        script.Parent.Text = "Decrease Graphics"
        local copy = game.Lighting.HighQuality:Clone()
        copy.Name = "LightPart" --You can name this whatever.
        copy.Parent = workspace --workspace and game.Workspace are the same thing.
        qualityhigh = true
    elseif qualityhigh then --Learning else and elseifs is the difference between level 1 and level 10 scripters.
        script.Parent.Text = "Increase Graphics"
        local copiedPart = workspace:FindFirstChild("LightPart")
        if copiedPart then --If the part exists (if not it returns nil)
            workspace.LightPart:Destroy()
            qualityhigh = false
        end
    end
end)
0
Doesn't work Dave_Robertson 20 — 7d
Ad

Answer this question