Help with creating a splitting mechanic

Hello developers, so I want to make my “entity” to have a splitting mechanic like when the entity rushes to its’ target position then splitting into three entities.

Here’s the code:
Code:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local JumpscareEvent = Events:WaitForChild("P145Jumpscare")

local p145 = {}

function p145.FindPlayers(model)
	local players = Players:GetPlayers()
	local characters = {}
	for i, player in ipairs(players) do
		if player.Character then
			table.insert(characters, player.Character)
		end
	end
	
	local overlapParams = OverlapParams.new()
	overlapParams.FilterType = Enum.RaycastFilterType.Include
	overlapParams.FilterDescendantsInstances = characters
	local collisions = workspace:GetPartsInPart(model.Smoke, overlapParams)
	
	for index, obj in ipairs(collisions) do
		if obj.Name == "HumanoidRootPart" then
			local rayDirection = obj.Position - model.P145.Position
			local result = workspace:Raycast(model.P145.Position, rayDirection)
			if result and result.Instance then
				local hit = result.Instance
				if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
					local FindPlayer = Players:GetPlayerFromCharacter(obj.Parent)
					if not FindPlayer.Died.Value and not FindPlayer.Hiding.Value then
						JumpscareEvent:FireClient(FindPlayer)
						FindPlayer.Died.Value = true
					end
				end
			end
		end
	end
end

function p145.LerpTo(model, target)
	local alpha = 0
	local speed = 235
	local distance = (model.PrimaryPart.Position - target.Position).Magnitude
	local relativeSpeed = distance / speed
	local startCFrame = model.PrimaryPart.CFrame
	local loop = nil
	local reachedTarget = Instance.new("BindableEvent")
	
	loop = RunService.Heartbeat:Connect(function(delta)
		p145.FindPlayers(model)
		local goalCFrame = startCFrame:Lerp(target.CFrame, alpha)
		model:PivotTo(goalCFrame)
		alpha += delta / relativeSpeed
		if alpha >= 1 then
			loop:Disconnect()
			reachedTarget:Fire()
		end
	end)
	
	reachedTarget.Event:Wait()
end

function p145.Navigate(model, prevNum, maxNum, generatedRooms, loopAmount)
	local currentLoop = 0
	local p145State = "isGoing" or "isComing"
	repeat
		currentLoop +=1
		if p145State == "isGoing" then
			task.wait(3)
			for i = prevNum, maxNum do
				local room = generatedRooms[i]
				p145.LerpTo(model, room.Entrance)

				local waypoints = room:FindFirstChild("Waypoints")
				if waypoints then
					for i=1, #waypoints:GetChildren() do
						p145.LerpTo(model, waypoints[i])
					end
				end
				p145.LerpTo(model, room.Exit)
			end
			p145State = "isComing"
		else
			task.wait(3)
			for i = maxNum, prevNum, -1 do
				local room = generatedRooms[i]
				p145.LerpTo(model, room.Exit)

				local waypoints = room:FindFirstChild("Waypoints")
				if waypoints then
					for i=#waypoints:GetChildren(), 1, -1 do
						p145.LerpTo(model, waypoints[i])
					end
				end
				p145.LerpTo(model, room.Entrance)		
			end
			p145State = "isGoing"
		end
	until currentLoop == loopAmount
end

function p145.New(number, generatedRooms)
	
	local enemyModel = game.ReplicatedStorage.Enemies.P145:Clone()
	
	local prevNum = number - 10
	local maxNum = number + 1
	local prevRoom = generatedRooms[prevNum]
	if not generatedRooms[maxNum] then
		maxNum = #generatedRooms
	end
	local maxRoom = generatedRooms[maxNum]
	local loopAmount = 3
	
	enemyModel:PivotTo(prevRoom.Entrance.CFrame)
	enemyModel.Parent = workspace
	enemyModel.P145.Ambience:Play()
	
	p145.Navigate(enemyModel, prevNum, maxNum, generatedRooms, loopAmount)
	
	enemyModel:Destroy()
end

return p145

Any help is appreciated, thanks. :wink:

1 Like

Nevermind, Just fixed my problem myself.

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