How to delete a part using a boolean

Yo so basically im trying to make a part get destroyed if a boolean’s value (thats inside the player) is set to true, the problem is that it doesn’t work for some reason:

local player = game.Players.LocalPlayer
if player.test.BoolValue.Value == true then
	script.Parent:Destroy()
end
4 Likes

This does work but not in the way you think. This will only get evaluated one time, which if the BoolValue’s Value is false then the conditional doesn’t pass and the instance doesn’t get destroyed. What you want to do instead is use the Changed event to check when the value changes.

Note that I’m making the assumption that you’re using a LocalScript, since LocalPlayer is nil (doesn’t exist) on the server.

player.test.BoolValue.Changed:Connect(function ()
    script.Parent:Destroy()
end
1 Like

alright ill try that real quick.

Alright it doesn’t work and also one different script stopped working idk why, could ya help?

Check if the Boolean value you are searching for exists. If yes use a Local Script. Also what you’ve written happens once. If the value is first false and after some time it goes to true nothing will break because the script has ended. You have to put a loop. Something like:

While true do
   if player.test.BoolValue.Value == true then
       script.Parent:Destroy()
end

I also suppose that the script’s parent is actually the part you want broken otherwise you have to define it and destroy it with the way you defined it like:

local part = game.Workspace.PartIWantBroken
...
  part:Destroy()
end
1 Like

Haven’t tested it so might not work.

local Players = game:GetService("Players")
local player = Players.LocalPlayer

player:WaitForChild("test"):WaitForChild("BoolValue").Changed:Connect(function()
    script.Parent:Destroy()
end)

If the script still doesn’t work, it’s because:

  1. Your script isn’t a LocalScript
  2. The LocalScript is somewhere it shouldn’t be (ServerStorage, workspace, etc)
  3. Your player doesn’t have a folder named “test”
  4. Your player doesn’t have a BoolValue named “BoolValue” inside of aforementioned folder

Also just a heads up that if you destroy a part with the LocalScript it won’t be replicated on the server, so you should use a ServerScript instead if your intention is for other players to see the part removed :slight_smile:

`

This will result in the game not responding for 5 seconds then ending the script. You should put a wait() instead of “true” in the “while” function or add a wait() elsewhere.

--You may need to modify the paths depending on script location and type
local player = game.Players.LocalPlayer
--Add WaitForChild statements here if test or BoolValue is nil
local val = player.test.BoolValue 
--Same goes for part if you decide to change the path
local part = script.Parent

--If the value is already true, destroy the object right away
if val.Value then part:Destroy() return end
--Else only wait for the value to change once, then destroy it
val.Changed:Once(function() part:Destroy() end)
1 Like

Thanks for correcting me. Programming is an area I still try to understand and learn. This should help

It works but the value is probably false when the game first runs and this code runs only once thus it just skips the destroy and never goes back, you should instead detect when it changes:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local bool = player:WaitForChild("test"):WaitForChild("BoolValue")

local connection -- Make sure to clear connections' variables afterwards to avoid memory leaks!
connection = bool:GetPropertyChangedSignal("Value"):Connect(function()
	if not bool.Value then return end -- If it's false we stop the function

	script.Parent:Destroy()
	-- connection:Disconnect() -- Destroy() disconnects this by itself!
	-- In other cases though, make sure to disconnect them.
	connection = nil -- Clear the variable
end)

God damn, sry for not responding i was afk for a bit, i didn’t know that many people will care to help me. Thanks! ill try all of the scripts yall gave me.

Alright so i don’t know if the scripts work since my datastore script messes everything up, could yall help me fix it?

That was really a bad lie. We are here to help anyone for anything. Seemingly stupid or not. You could come asking how can I make a color change using a script and you would still get responses.
We are here to help. We are not here to read and not care! This is why the Roblox community is still strong. Keep up the good work soldier!!!