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

How would I make this affect multiple objects named "Tree"?

local Players = game:GetService("Players").LocalPlayer
local Tool = script.Parent
local gun = script.Parent.Handle
local mouse = Players:GetMouse()
local deb = true
Tool.Equipped:Connect(function()
	script.Parent.Beam.Enabled = false
    mouse.Button1Down:Connect(function()
		if game.Workspace.Tree.Name == "Tree" then
			script.Parent.Beam.Enabled = true
			script.Parent.Beam.Attachment0 = Tool.Handle.Main_Attatchment
			script.Parent.Beam.Attachment1 = workspace.Tree.Atta.TreeAttatchment
			workspace.Tree.Atta.TreeAttatchment.Position = Vector3.new(mouse.Target.CFrame)
			wait(1.5)
			script.parent.Beam.Enabled = false
		end
	end)
end)

Right now this only affects one object named tree how would I make it affect more?

3 comments

0

I recommend using Tool.Activated event in replace of the Line 8 use of Button1Down.

0

LOLLLLLLLLL

0

I named it tree :]

2 answers

✓ Accepted answer

Use a for loop to get the children of an object (This case, it is workspace) and find the ones named tree. If it is a tree, do stuff with the tree.

for _, obj in pairs(workspace:GetChildren()) do
	if obj.Name == "Tree" then
		script.Parent.Beam.Enabled = true
		script.Parent.Beam.Attachment0 = Tool.Handle.Main_Attatchment
		script.Parent.Beam.Attachment1 = workspace.Tree.Atta.TreeAttatchment
		workspace.Tree.Atta.TreeAttatchment.Position = Vector3.new(mouse.Target.CFrame)
		wait(1.5)	
		script.parent.Beam.Enabled = false
	end
end

More on for loops

1 comment

0

Either that or place all your trees in the same parent so you just need to get that objects children

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