Help needed for a beam that chains between humanoids!

Im trying to recreate the following effect:
https://gyazo.com/c0efeb99a7268068ff0b765cedfe90d5

Its a beam that chains to the nearest enemy, and continues onward if there is another nearby.
Ive tried making it a couple of times, but cant get anything to work.
Sadly theres nothing on the devforum!

One of my attempts:
https://gyazo.com/10c86d4dc2bbdc10b22cc013f3fa3397

Thank you in advance!

if v:FindFirstChild(“Humanoid”) then
if script.Parent then
if v ~= script.Parent.Parent then
if (v.PrimaryPart.Position - script.Parent.Position).magnitude <= 20 then
if not v:FindFirstChild(“Used”) then
local rope = Instance.new(“RopeConstraint”, workspace)
local a1 = Instance.new(“Attachment”,script.Parent)
local a2 = Instance.new(“Attachment”,v.PrimaryPart)
local value = Instance.new(“BoolValue”, v)
value.Name = “Used”
value.Value = true
rope.Length = (v.PrimaryPart.Position - script.Parent.Position).magnitude
rope.Enabled = true
rope.Visible = true
rope.Attachment0 = a1
rope.Attachment1 = a2
script:Clone().Parent = v.PrimaryPart
script:Destroy()
wait(1.5)
value:Destroy()
wait(1.5)
rope:Destroy()
end
end
end
end
end
end
wait(0.5)
script:Destroy()

This should create a rope going through all the enemies. You probably need to edit it a bit for your game tho.

Edit: I think it works with a beam too. I’m not sure.

local Debris = game:GetService("Debris")
local Players = game:GetService("Players")

--[[Allows us to get the next closest character from our chained enemy, initially the player should be used.
A blacklist is used to ensure it doesn't chain back to a previous enemy.]]
local function GetNextClosestTargetWithBlacklist(PreviousTarget, Blacklist)
	local NewTarget = nil
	local MinDistance = 30
	
	for _,Child in workspace:GetChildren() do
		if Child.ClassName == "Model" and not Blacklist[Child] then
			local HumanoidRootPart = Child:FindFirstChild("HumanoidRootPart")
			local Humanoid = Child:FindFirstChildOfClass("Humanoid")
			
			if Humanoid and Humanoid.Health > 0 and HumanoidRootPart then
				local Magnitude = (PreviousTarget.HumanoidRootPart.Position - HumanoidRootPart.Position).Magnitude
				if Magnitude < MinDistance then
					NewTarget = Child
					MinDistance = Magnitude
				end
			end
		end
	end
	
	return NewTarget
end

--Visualize the beam between 2 root parts
local function CreateBeam(Root1, Root2)
	local Attachment0, Attachment1 = Instance.new("Attachment", Root1), Instance.new("Attachment", Root2)
	local Beam = Instance.new("Beam")
	Beam.Attachment0, Beam.Attachment1 = Attachment0, Attachment1
	Beam.Parent = Root1
	
	--Schedules the beam to be cleaned up once it's no longer needed
	Debris:AddItem(Beam, 1)
end

--Cast the spell
local Blacklist = {[Players.wf_sh.Character] = true}
local PreviousTarget = Players.wf_sh.Character

local CurrentTarget = GetNextClosestTargetWithBlacklist(PreviousTarget, Blacklist)
while CurrentTarget do
	CreateBeam(PreviousTarget.HumanoidRootPart, CurrentTarget.HumanoidRootPart)
	CurrentTarget.Humanoid:TakeDamage(40)
	
	Blacklist[CurrentTarget] = true
	PreviousTarget = CurrentTarget
	CurrentTarget = GetNextClosestTargetWithBlacklist(PreviousTarget, Blacklist)
end

1 Like

It worked! Thank you so much :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.