Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Only makes one block a variable, not all of them?

for i, object in pairs(game.Workspace.topLawn:GetChildren()) do
	lawnPiece = object
end

function onMow(hit)
	if hit.Parent.Name == game.ServerStorage.lawnMower.Name then
		lawnPiece:Destroy()
	end
end

lawnPiece.Touched:connect(onMow)

I'm not sure if the title makes sense or not, but basically, it's only making ONE block the variable, instead of having all 50 of them.

Sorry if I'm bad at wording things, and thanks in advance!

1 answer

When you say :

for i, object in pairs(game.Workspace.topLawn:GetChildren()) do
    lawnPiece = object
end

You are just switching local lawnPiece into one of topLawn's objects then quickly assigning it to another one.

You are just doing this:

lawnPiece = game.Workspace.topLawn.Part
lawnPiece = game.Workspace.topLawn.Part
lawnPiece = game.Workspace.topLawn.Part
lawnPiece = game.Workspace.topLawn.Wedge
lawnPiece = game.Workspace.topLawn.Part
lawnPiece = game.Workspace.topLawn.Part

You cannot assign all of those into one variable without making a table. But in your case, it is better to just run the loop to destroy all of topLawn's descendants in the function itself.

Full complete code:

function onMow(hit)
    if hit.Parent.Name == game.ServerStorage.lawnMower.Name then
	for i, object in pairs(game.Workspace.topLawn:GetChildren()) do
	    object:Destroy()
	end
    end
end

lawnPiece.Touched:connect(onMow)

You might want to consider doing this instead.

function onMow(hit)
    if hit.Parent.Name == game.ServerStorage.lawnMower.Name then
	workspace.topLawn:Destroy()
    end
end

lawnPiece.Touched:connect(onMow)

But I am not entirely sure you want to do next so you might not want to use the extra code above. The code above just destroys the whole topLawn in one tick.

This discussion is preserved as it was. New answers, comments, and votes are closed.