Destroy object locally with specific name on join

  1. What do you want to achieve? gates destroy when a player joins based on his Value

  2. What is the issue? the gates dont destroy themselves

  3. What solutions have you tried so far? i tried to use for loops but it didnt work

heres a part of the code which is supposed to destroy the gates on join

local player = game:GetService("Players").LocalPlayer
local gatesValue = player.Data:FindFirstChild("Gates")
local world = game:GetService("Workspace")

for i, v in pairs(world:GetDescendants()) do
	if v.Name == "Gate"..tostring(gatesValue.Value <= gatesValue.Value) then
		v:Destroy()
	end
end

Why did you write this? tostring(gatesValue.Value <= gatesValue.Value) will always return the string "true" because obviously gatesValue.Value == gatesValue.Value is true, so you’re essentially checking if the gate is named "Gatetrue". Is that what your gates are named?

the value is an integer and im checking if there is a gate which is named Gate1 and another named Gate2 which the script will check from the values

<= will return a true/false value as it’s a comparison.

Try this.

local player = game:GetService("Players").LocalPlayer
local gatesValue = player.Data:FindFirstChild("Gates")
local world = game:GetService("Workspace")

for i, v in pairs(world:GetDescendants()) do
	if v.Name == "Gate1" or v.Name == "Gate2" then
		v:Destroy()
	end
end

Instead of just relying on the client to destroy the gate (as it could accidentally be brought back on the server), you could simply create a Collision Group for all players who are not permitted to pass the gate when their character loads.

but if i do that the gates would just instantly destroy on join and the values wont do anything