For function don't working in local script

for _, i in pairs(workspace.Area1Melons:GetChildren()) do
	if i:IsA("MeshPart") then
		i.Touched:Connect(function()
			i:Destroy()
		end)
	end
end

what error i did?

Where is the script located?

Also it is not a “for function”, this is a for loop.

script is located in StarterPlayerScripts, or its need to be in StarterCharacterScripts?

When do you want this code to run? Also get any errors at all?

i want do when player touching watermelon, watermelon destroys but it don’t works

You are creating a new connection only for the first part, if you want to know if one part touched in a for loop you should use the GetTouchingParts() function

for _, i in pairs(workspace.Area1Melons:GetChildren()) do
	if i:IsA("MeshPart") then
		if #i:GetTouchingParts() > 0 then
			i:Destroy()
		end
	end
end

don’t working, maybe because i spawning parts from localscript too?

Are you wanting to destroy when the game starts or while the game is running?

you can use the while loop to destroy while the game is running

while task.wait() do
	for _, i in pairs(workspace.Area1Melons:GetChildren()) do
		if i:IsA("MeshPart") then
			if #i:GetTouchingParts() > 0 then
				i:Destroy()
			end
		end
	end
end

i want while game is running, and this again don’t works

Remembering that the local script will only destroy for the player who executed it, if you want this to be replicated on the server too you would have to use a RemoteEvent

i need it only for client-side

Try a script which clones another script and puts it in each melon (although idk how it would differ from your code)

for _, i in pairs(workspace.Area1Melons:GetChildren()) do
if i:IsA("MeshPart") then
local newscript = game.ReplicatedStorage.MelonScript:Clone()
newscript.Parent = i

end
end

and the MelonScript in RStorage would just be

script.Parent.Touched:Connect(function()
script.Parent:Destroy()
end)

don’t working again. idk what i can do

put this in some Melon

print(script.Parent.ClassName)

What was the output?

how? im stupid in local or… server script?

in a server script in some child of the workspace.Area1Melons

it prints meshpart, btw any scripts don’t working in watermelons

You are using pairs() which only works with dictionaries. GetChildren() returns an array so you should use ipairs() instead.

for _, i in ipairs(workspace.Area1Melons:GetChildren()) do

That’s not the case, pairs works with any table.

This probably wont help at all but try i, v instead of _, i?