Attachment beam that clones itself several times to players

I wanted to ask how I could go about creating a script that clones an attachment, which has several beams inside the attachment, in a player 8 times and have the cloned attachment’s beams attachment1 be 8 other randomly chosen player’s torso while keeping their attachment0 the same. Not sure if my explanation on what I’m looking for is clear but all help is appreciated!

My current code

local Beam = Model["Left Arm"].BeamAttachment
BeamAttack:GetMarkerReachedSignal("Start"):Connect(function() -- Animation plays prior
					for i=1,8 do
						local players = game.Workspace:GetChildren() -- I know these are wrong
						local targeted = players[math.random(1, #game.Workspace:GetChildren())]
						
					end			
				end)
2 Likes

Here’s a solution you could possibly implement:

task.wait(40) -- this is just here so my pc has time to load all players, you wont need this as all players will already be loaded when the player attacks

-- Reference Objects
local Players = game.Players
local attacker = Players["Player1"] -- set this to the player object of the attacking player
local char = attacker.Character
local beam = script.Beam -- set this to the beam object
local beamAttachment = char:WaitForChild("Left Arm")["LeftGripAttachment"] -- set this to the attachment inside the attacking player

-- Tables
local PotentialTargetPlayers = {}
local TargetPlayers = {}

-- Constants
local numTargetPlayers = 8 -- how many players will be randomly chosen
local BEAM_DURATION = 5 -- how long the beam lasts (seconds)

local function Attack()
	table.clear(PotentialTargetPlayers) -- reset tables
	table.clear(TargetPlayers)

	for i = 1,#Players:GetChildren() do -- setup list of possible targets
		if Players:GetChildren()[i] == attacker then continue end -- exclude the attacking player
		
		table.insert(PotentialTargetPlayers,Players:GetChildren()[i])
	end

	if #Players:GetChildren() > numTargetPlayers then
		for i = 1,numTargetPlayers do
			local target = PotentialTargetPlayers[math.random(1,#PotentialTargetPlayers)] -- find random target
			
			table.remove(PotentialTargetPlayers,table.find(PotentialTargetPlayers,target)) -- remove target from list of possible targets
			table.insert(TargetPlayers,target) -- add target to target table
		end
	else
		for i = 1,#Players:GetChildren() - 1 do
			local target = PotentialTargetPlayers[math.random(1,#PotentialTargetPlayers)] -- find random target
			
			table.remove(PotentialTargetPlayers,table.find(PotentialTargetPlayers,target)) -- remove target from list of possible targets
			table.insert(TargetPlayers,target) -- add target to target table
		end
	end

	for i = 1,#TargetPlayers do -- create a beam for every target, attach beam to player1 and targets
		local beam = beam:Clone()
		beam.Parent = beamAttachment
		beam.Attachment0 = beamAttachment
		beam.Attachment1 = TargetPlayers[i].Character:WaitForChild("HumanoidRootPart")["RootAttachment"]
		
		task.delay(BEAM_DURATION,function() -- removes beam after set time - delete this if you don't want it
			beam:Destroy()
		end)
	end
end

Attack()

A couple things to note:

  • This script creates 8 beams instead of 8 attachments and attaches each of the beams to the player that is attacking and to each of the players being attacked.
  • I added in some code that destroys the beam after a few seconds, you can modify or delete this if you are handling it differently.
  • Make sure to change the values under Reference Objects to the values you are using in your script

Script in action:

Try it out and let me know how it goes!

1 Like