I am trying to make a scary monster for my friend’s game, here is the code, and video footage:
Utility = require(game.ReplicatedStorage.Utility)
local RunService = game:GetService("RunService")
local currentRoom, currentRoomModel = Utility.GetCurrentRoom()
local lastRoom, lastRoomModel = Utility.GetLastRoom()
local latestRoom, latestRoomModel = Utility.GetLastRoom()
function FindPlayers(part)
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
local nearcharacters = workspace:GetPartBoundsInRadius(part.Parent:WaitForChild("Torso").Position, 100, overlapParams)
local collisions = workspace:GetPartBoundsInRadius(part.Parent:WaitForChild("Torso").Position, 9, overlapParams)
for index, obj in ipairs(nearcharacters) do
if obj.Name == "HumanoidRootPart" then
if obj.Parent == game.Players.LocalPlayer.Character then
local rayDirection = obj.Position - part.Parent:WaitForChild("Torso").Position
local result = workspace:Raycast(part.Parent:WaitForChild("Torso").Position, rayDirection)
if result and result.Instance then
local hit = result.Instance
if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
if not obj.Parent.Humanoid.Sit and obj.Parent.Humanoid.Health > 0 and obj.Parent.Humanoid.Health < 10000 then
obj.Parent:WaitForChild("Humanoid").Health = 0
end
end
end
end
end
end
for index, obj in ipairs(collisions) do
if obj.Name == "HumanoidRootPart" then
if obj.Parent == game.Players.LocalPlayer.Character then
local rayDirection = obj.Position - part.Parent:WaitForChild("Torso").Position
local result = workspace:Raycast(part.Parent:WaitForChild("Torso").Position, rayDirection)
if result and result.Instance then
local hit = result.Instance
if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
if not obj.Parent.Humanoid.Sit and obj.Parent.Humanoid.Health > 0 and obj.Parent.Humanoid.Health < 10000 then
obj.Parent:WaitForChild("Humanoid").Health = 0
end
end
end
end
end
end
end
function LerpTo(part, target, spawning, isWaypoint)
local alpha = 0
local speed = 200
local targetPosition = target.Position + Vector3.new(0,3.5,0)
if spawning then
speed *= 1
end
local distance = (part.Parent:WaitForChild("Torso").Position - targetPosition).Magnitude
local relativeSpeed = distance / speed
local startCFrame = part.Parent:WaitForChild("Torso").CFrame
local loop = nil
local reachedTarget = Instance.new("BindableEvent")
loop = RunService.Heartbeat:Connect(function(delta)
local lastRoom, lastRoomModel = Utility.GetLastRoom()
local latestRoom, latestRoomModel = Utility.GetLatestRoom()
if spawning then
target = lastRoomModel.Entrance
end
local goalCFrame
if isWaypoint then
goalCFrame = startCFrame:Lerp(target.CFrame + Vector3.new(0,3.5,0), alpha)
else
goalCFrame = startCFrame:Lerp(target.CFrame, alpha)
end
part:PivotTo(goalCFrame)
if spawning then
part:PivotTo(CFrame.new(target.Position.X, goalCFrame.Y, target.Position.Z))
end
alpha += delta / relativeSpeed
if alpha >= 1 then
loop:Disconnect()
reachedTarget:Fire()
end
end)
reachedTarget.Event:Wait()
end
function Navigate(part, generatedRooms, spawning)
local errorCount = 0
local spawning = spawning
local room = nil
local currentWaypoint = nil
local currentWaypointFolder = nil
local success, result = pcall(function()
local lastRoom, lastRoomModel = Utility.GetLastRoom()
local latestRoom, latestRoomModel = Utility.GetLatestRoom()
if room == nil then
room = lastRoom
end
for i=room, 10000, 1 do
room = i
lastRoom, lastRoomModel = Utility.GetLastRoom()
latestRoom, latestRoomModel = Utility.GetLatestRoom()
if spawning then
LerpTo(part, lastRoomModel.Exit, true, false)
spawning = false
end
local curRoomModel = workspace.GeneratedRooms:FindFirstChild(i)
LerpTo(part, curRoomModel.Entrance, false, false)
local waypointFolders = {}
for _, v in pairs(curRoomModel:GetChildren()) do
if v.Name == "Waypoints" then
table.insert(waypointFolders, v)
end
end
if #waypointFolders > 0 then
local waypoints = currentWaypointFolder or Utility.GetRandomFromTable(waypointFolders, 1)
if currentWaypoint then
for w=currentWaypoint, #waypoints:GetChildren(), 1 do
LerpTo(part, waypoints[w], false, true)
currentWaypoint = w
end
else
for w=1, #waypoints:GetChildren(), 1 do
LerpTo(part, waypoints[w], false, true)
currentWaypoint = w
end
end
else
currentWaypoint = nil
currentWaypointFolder = nil
end
LerpTo(part, curRoomModel.Exit, false, false)
currentWaypoint = nil
currentWaypointFolder = nil
if tonumber(curRoomModel.Name) == latestRoom then
break
end
end
end)
if not success then
if errorCount < 10 then
errorCount += 1
print(result)
Navigate(part, generatedRooms)
else
warn(result)
script.Parent:Destroy()
end
end
end
function NavigateBackwards(part, generatedRooms, spawning)
local errorCount = 0
local spawning = spawning
local room = nil
local currentWaypoint = nil
local currentWaypointFolder = nil
local success, result = pcall(function()
local lastRoom, lastRoomModel = Utility.GetLastRoom()
local latestRoom, latestRoomModel = Utility.GetLatestRoom()
if spawning then
LerpTo(part, latestRoomModel.Exit, true, false)
spawning = false
end
lastRoom, lastRoomModel = Utility.GetLastRoom()
latestRoom, latestRoomModel = Utility.GetLatestRoom()
if room == nil then
room = latestRoom
end
for i=room, 0, -1 do
room=i
lastRoom, lastRoomModel = Utility.GetLastRoom()
latestRoom, latestRoomModel = Utility.GetLatestRoom()
local curRoomModel = workspace.GeneratedRooms:FindFirstChild(i)
LerpTo(part, curRoomModel.Exit, false, false)
local waypointFolders = {}
for _, v in pairs(workspace.GeneratedRooms:FindFirstChild(i):GetChildren()) do
if v.Name == "Waypoints" then
table.insert(waypointFolders, v)
end
end
if #waypointFolders > 0 then
local waypoints = currentWaypointFolder or Utility.GetRandomFromTable(waypointFolders, 1)
currentWaypointFolder = waypoints
if currentWaypoint ~= nil then
for w=currentWaypoint, 1, -1 do
LerpTo(part, waypoints[w], false, true)
currentWaypoint = w
end
else
for w=#waypoints:GetChildren(), 1, -1 do
LerpTo(part.Parent, waypoints[w], false, true)
currentWaypoint = w
end
end
else
currentWaypoint = nil
currentWaypointFolder = nil
end
LerpTo(part, curRoomModel.Entrance, false, false)
currentWaypoint = nil
currentWaypointFolder = nil
if tonumber(curRoomModel.Name) == lastRoom then
break
end
end
end)
if not success then
if errorCount < 10 then
errorCount += 1
NavigateBackwards(part.Parent:WaitForChild("Torso"), generatedRooms, false)
else
warn(result)
script.Parent:Destroy()
end
end
end
script.Parent:PivotTo(lastRoomModel.Entrance.CFrame + Vector3.new(0,-3000,0))
print("Blitz spawned")
script.Parent.hunt.Value = true
task.wait(1)
Navigate(script.Parent.Torso, workspace.GeneratedRooms, true)
despawnLength = 2
--------
entity = script.Parent
entity.hunt.Value = false
wait(despawnLength)
script.Parent:Destroy()