Issue with ui and deleting parents

Basically, I want to make it so that if the component doesn’t have the names of the following then it gets deleted, but when I play the parts just get deleted, I’m pretty sure I’ve got it correct since ~= is is not equal to. Can someone help. I’ve attached image of what it looks like before and after and the script. Please just tell me what the error is or how you think I can fix it; I want to learn so this doesn’t happen again.

local componentsParent = script.Parent
local configurationEvent = game:GetService("ReplicatedStorage"):WaitForChild("ConfigurationEvent")

local function load()
	configurationEvent:FireServer()
end

while wait(1) do
	print("Check started")
	for i, v in pairs(componentsParent:GetChildren()) do
		if v.Name ~= "ButtonHandler" or "ConfigurationHandler" or "ComponentHandler" or "TeamsFrame" or "PlayButton" or "TeamsButton" or "BackgroundImage" or "GameTitle" then
			warn("Destroyed "..v.Name)
			v:Destroy()
		end
	end
end

You have to add v.Name ~= before every new name.
Otherwise you’re checking if a string evaluates as true.
(I believe a string is true when it is not empty, but don’t quote me on this.)
Also use and instead of or.

Edit: a cleaner way of doing this would be by having a table where each value is a string you want to check and using table.find().

2 Likes

You need to do it for each string. Bruh.

Try this

local componentsParent = script.Parent
local configurationEvent = game:GetService("ReplicatedStorage"):WaitForChild("ConfigurationEvent")

local dontDestroyList = {
    "ButtonHandler",
    "ConfigurationHandler",
    "ComponentHandler",
    "TeamsFrame",
    "PlayButton",
    "TeamsButton",
    "BackgroundImage",
    "GameTitle"
}

local function load()
	configurationEvent:FireServer()
end

while wait(1) do
	print("Check started")
	for i, v in pairs(componentsParent:GetChildren()) do
		if not table.find(dontDestroyList, v.Name) then
			warn("Destroyed "..v.Name)
			v:Destroy()
		end
	end
end

try this…

local configurationEvent = game:GetService("ReplicatedStorage"):WaitForChild("ConfigurationEvent")
local componentsParent = script.Parent

local function load()
	configurationEvent:FireServer()
end

while wait(1) do
	print("Check started")
	for i, v in pairs(componentsParent:GetChildren()) do
		if (v.Name ~= "ButtonHandler" or v.Name ~= "ConfigurationHandler" or v.Name ~= "ConfigurationHandler" or v.Name ~= "TeamsFrame" or v.Name ~= "PlayButton" or v.Name ~= "TeamsButton" or v.Name ~= "BackgroundImage" or v.Name ~= "GameTitle") then
			warn("Destroyed ".. v.Name)
			v:Destroy()
		end
	end
end

Thanks for explaining how to do it and why. I will try to experiment on using tables. Thank you.

1 Like

Thank you. That was the solution.

Awesome, thanks, I’ll experience with using a table.

Your table worked perfectly, thank you.

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