How would I check if I touched something with a certain name from a localscript

local haveball = false
local Part = workspace.ballgiver



Part.Touched:Connect(function() 
	print("got the ball")
        Part:Destroy()
	if haveball == false then haveball = true
		end
	
	if haveball == true then

		Humanoid.Parent.Basketball.Transparency = 0
	end
	
end)

I am trying to make a basketball game, this localscript located in StarterPlayerScripts is supposed to delete everything in workspace named “Basketball” when “Basketball” is touched, however, this does not work for some reason.

.Touched passes the BasePart which collides with Part as a parameter:

Part.Touched:Connect(function(hit)
	print(hit.Name) --name of BasePart which interacted with Part
end)

which you can then use to check if it’s the basketball:

if hit.Name == "Basketball" then
	hit:Destroy()
end

You are doing this with a local script, this will happen only in the client. Also if you want everything in workspace going by the name basketball you can do a for loop,

for _, v in pairs(workspace:GetDescendants()) do
    if v:IsA("Part") and v.Name == "Basketball" then
        v:Destroy()
    end
end

moving on, insert a ServerScript into ServerScriptService and insert a RemoteEvent into replicatedStorage, rename it to something like DestroyBasketball and then get it inside a script.

-- add onto local script --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DestroyBEvent = ReplicatedStorage:WaitForChild("DestroyBasketball") -- name of event
-- additional variables

Part.Touched:Connect(function(hit)
    DestroyBEvent:FireServer(Part) -- This will be helpful for the server to track down the ball
    -- rest of code here but remove destroy code
end)

Now onto the serverScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DestroyBEvent = ReplicatedStorage:WaitForChild("DestroyBasketball") -- name of event

DestroyBEvent.OnServerEvent:Connect(function(player,Part) -- first parameter is player, who fired it? then the part comes, try to remember this for future references
    Part:Destroy()
    -- OR YOU CAN DO THE LOOP WHICH DELETS ALL BASKETBALLS IN WORKSPACE/ OR BOTH
    for _, v in pairs(workspace:GetDescendants()) do
        if v:IsA("Part") and v.Name == "Basketball" then
            v:Destroy()
        end
    end
end)

hopefully this helped :happy1:
Yours sincerely, @oscoolerreborn

for _, v in pairs(workspace:GetDescendants()) do
    if v:IsA("Part") and v.Name == "Basketball" then
        v:Destroy()
    end
end

how would i add this to my current Part.touched script?