How would i delete a part if a boolean's value is set to true

Hello so i got this script inside a part:

local plr = game.Players.LocalPlayer
local partiwanttodestroy = script.Parent
while wait(0.1) do
	if plr:FindFirstChild("test").BoolValue.Value == true then
		partiwanttodestroy:Destroy()
	end
end

but for some reason it doesn’t work, could yall help?

1 Like

This seems to be a LocalScript since you’re using LocalPlayer. You can’t use a LocalScript in the Workspace.

Either move the LocalScript to somewhere that runs LocalScripts (StarterPlayerScripts, StarterGui, etc.) or make the LocalScript into a Script and adapt it so it doesn’t use LocalPlayer.

okay i put the script in “StarterPlayerScripts”

local plr = game.Players.LocalPlayer
local partiwanttodestroy = game.Workspace.Ok
while wait(0.1) do
	if plr:FindFirstChild("test").BoolValue.Value == true then
		partiwanttodestroy:Destroy()
	end
end

but i get "Ok is not a valid member of Workspace “Workspace” in the output.
image

Since LocalScripts load on the client, you have to wait for things to load in on the client. Think about whenever you join a game with a lot of parts, sometimes you’ll notice that the assets don’t all load in until later! That’s what’s happening here.

To fix this, wait for part “Ok” to load in using WaitForChild:

local plr = game.Players.LocalPlayer
local partiwanttodestroy = game.Workspace:WaitForChild("Ok")
while wait(0.1) do
	if plr:FindFirstChild("test").BoolValue.Value == true then
		partiwanttodestroy:Destroy()
	end
end

It actually works, Thank you so much! (i was trying to get the thing to work with a datastore script and it finally works)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.