Need Help with this Local Script

script.Parent.MouseButton1Down:Connect(function()
	for i, v in pairs(game.Workspace.Vegetation.Model:GetDescendants()) do if not v:IsA("BasePart") then continue end
		v.Transparency = 1
	end
end)
2 Likes

If thatā€™s the case. Youā€™d need to use the Transparency value for that, and not :Destroy().

Use these code snippets:

1 Like
local enabled = 1
script.Parent.MouseButton1Down:Connect(function()
    for i, v in pairs(game.Workspace.Vegetation.Model:GetDescendants()) do if not v:IsA("BasePart") then continue end
		v.Transparency = enabled
	end
    enabled = math.abs(enabled - 1)
end)
2 Likes

Just some advice, instead of using an enabled value that is true or false, it can be helpful to set it as the value you want it to be when it is true/false. That way you have no annoying if statements.

2 Likes

Try this

local clicked = false

script.Parent.MouseButton1Down:Connect(function()
	if clicked then
		clicked = false
		for i, v in pairs(game.Workspace.Vegetation:GetChildren()) do
			v.Parent = game.ReplicatedStorage
		end
	else
		clicked = true
		for i, v in pairs(game.Workspace.Vegetation:GetChildren()) do
			v.Parent = game.Workspace
		end
	end
end)
2 Likes

It Did Nothing Sorry and whats the ReplicatedStorage For?

1 Like
local clicked = false

script.Parent.MouseButton1Down:Connect(function()
	if clicked then
		task.wait(1)
		clicked = false
		for i, v in pairs(game.Workspace.Vegetation:GetChildren()) do
			v.Parent = game.ReplicatedStorage
		end
	else
		task.wait(1)
		clicked = true
		for i, v in pairs(game.Workspace.Vegetation:GetChildren()) do
			v.Parent = game.Workspace
		end
	end
end)

So we parent the trees to replicatedstorage to hide the trees from workspace. Can you recopy the script? I just fixed the mistakes.

2 Likes

What If I Put That into the ServerStorage? Will that affect It?

You canā€™t access ServerStorage from a LocalScript, so that script would likely error.

1 Like

What you want is quite complicated honestly if you want to make every part transparent

Try this I guess

function transparency(parent)
    for i, part in pairs(parent:GetChildren()) do
      if part.ClassName == "Part" then
           part.Transparency = 1
   end
           if #part:GetChildren > 0 then
               transparency(part)
           end
end

script.Parent.MouseButton1Down:Connect(function()

     transparency(game.Workspace.Vegetation)


end)

What Iā€™m pretty sure this should do is when you call the function, if goes through all the children of the thing you have as ā€œparentā€ and checks if itā€™s a part
If it is, it makes it transparent
Then it checks if that part has any children
If so, it calls the function again for part and repeats itself
I think this is called a recursive function

Then of course when you click the button, you kickstart it by setting the ā€œVegetationā€ as the parent and looping through its children

Also if you want this to reverse and make stuff visible, just make it so you make the function again but change part.Transparency = 1 to part.Transparency = 0
Thereā€™s an easier way to do that instead of making a whole new function (just add a parameter) but whatever

1 Like

Have you tried this yet, @Sim_Q1?

It Never Worked, But Iā€™ll see what the other oneā€™s are like.

Did you try my script? Any errors in the output? Btw local scripts never work on the server which includes services like serverscriptservice. I donā€™t think scripts can ever run in serverstorage because itā€™s for storing things.

Try:

local tree = workspace.Vegetation.Model
local initialParent = tree.Parent
local intendedParent = game:GetService("ReplicatedStorage")

local hidden = false

script.Parent.MouseButton1Down:Connect(function()
	if hidden then
		tree.Parent = initialParent
	else
		tree.Parent = intendedParent
	end
	hidden = not hidden
end)

As long as Iā€™m understanding that the item youā€™re trying to hide is workspace.Vegetation.Model, this should work. Tested myself just now. @CriticalGorilla had a good idea with parenting to ReplicatedStorage since making a model transparent is a situational task.

let me See the output, and I will get back to you.

Oh if it didnā€™t work thenā€¦ try this?

local vegetationClone = game.Workspace.Vegetation:Clone()


script.Parent.MouseButton1Down:Connect(function()

game.Workspace.Vegetation:Destroy()


end)

Then, when you want the vegetation back, just add

local vegetationClone = game.Workspace.Vegetation:Clone()


script.Parent.MouseButton1Down:Connect(function()

game.Workspace.Vegetation:Destroy()

--some sort of if statement to check if the vegetation is currently
--present, and if it isnt then run the following code:
local newVegetation = vegetationClone:Clone()
newVegetation.Parent = game.Workspace


end)

Simpler and should leave less room for error, although likely still quite demanding to undo the whole thing at least

I am Not Finding any errors, But Should I Can to a Normal Script and Not a Localscript?

Will that make it Better?

If you want everyone (every single player) to no longer see the model you are trying to hide, then yes, but that would require multiple scripts (including a LocalScript) and is likely not what you want.

ohā€¦ Then I just Wasted everyoneā€™s time Then Sorry I thought you could do it just With One Script.

I recommend reading up on RemoteEvents (again, if you want everyone elseā€™s trees to disappear when any player clicks this button).

2 Likes