How to make my monster that rebounds?

Hello, I’m making a monster that rebounds 1 to 10 times, but I don’t know how.

ModuleScript:

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

local partyambush = {}

function partyambush.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.Partyrush.Position
			local result = workspace:Raycast(model.Partyrush.Position, rayDirection)
			if result and result.Instance then
				local hit = result.Instance
				if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
					obj.Parent.Humanoid.Health = 0
				end
			end
		end
	end
end

function partyambush.LerpTo(model, target)
	local alpha = 0
	local speed = 67
	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)
		partyambush.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 partyambush.Navigate(model, prevNum, maxNum, generatedRooms)
	for i = prevNum, maxNum do
		local room = generatedRooms[i]
		partyambush.LerpTo(model, room.Entrance)
		
		local waypoints = room:FindFirstChild("Waypoints")
		if waypoints then
			for i = 1, #waypoints:GetChildren() do
				partyambush.LerpTo(model, waypoints[i])
			end
		end
		
		partyambush.LerpTo(model, room.Exit)
	end
end

function partyambush.New(number, generatedRooms)
	
	local enemyModel = game.ReplicatedStorage.Enemies.Partyambush:Clone()
	
	local prevNum = number - 1
	local maxNum = number + 1
	local prevRoom = generatedRooms[prevNum]
	if not generatedRooms[maxNum] then
		maxNum = #generatedRooms
	end
	local maxRoom = generatedRooms[maxNum]
	
	enemyModel:PivotTo(prevRoom.Entrance.CFrame)
	enemyModel.Parent = workspace
	enemyModel.Partyrush.Laugh:Play()
	
	partyambush.Navigate(enemyModel, prevNum, maxNum, generatedRooms)
	
	local lastDoor = maxRoom.Door
	lastDoor.OpenEvent:Fire()
	
	enemyModel:Destroy()
end

return partyambush

Any help is appreciated, thanks.

4 Likes

Why is there no one replying?

2 Likes

Maybe because you haven’t specified any problems in your script. We cannot give you full systems, only give tips to work off of. I suggest using TweenService and Pathfinding.

3 Likes

I need to do this using lerp to move him, he reaches the exit part of the room, then come back to where he spawned, and then go back to the exit part of the room again, this process repeats 3 times and after that, the monster will stop moving, then destroying it.

2 Likes

I repeat, I suggest using a lerping system and pathfinding. For the “repeating 3 times” part, use a special type of for loops, which would be the ones that add a value to an Index.

for i = 1, 3, 1 do
-- stuff
end
2 Likes

How do I do this using for do loops?

1 Like

The loop above is an example of what kind you should use. Where

-- stuff

is written, you write there your code that makes the entity go to the last door then returns.

1 Like