I’m making a horror game as my first project to learn how to code on roblox and I’ve run into a problem with the player kinda messing with the monsters pathfinding/balance. If there is anyway to fix this please tell me.
(Where I welded the player with the monster is in the “Hunt” function.)
-- services
local players = game:GetService("Players")
local pfs = game:GetService("PathfindingService")
-- variables
local rig = script.Parent
local randomplr = nil
local sound = rig.Torso.Footsteps
local att = rig["Right Arm"].Grab
-- states
local hunting = false
-- functions
local function getrandomplr()
local plrs = players:GetPlayers()
while #plrs == 0 do
wait()
print(plrs)
plrs = players:GetPlayers()
end
if #plrs > 0 then
randomplr = plrs[math.random(1, #plrs)]
print(randomplr)
return randomplr
end
end
getrandomplr()
local function checkforchar(character)
local rayorigin = rig:FindFirstChild("HumanoidRootPart").Position
local rayDirection = (character.HumanoidRootPart.Position - rayorigin).Unit * 40
local raycastresult = workspace:Raycast(rayorigin, rayDirection, RaycastParams.new())
if raycastresult then
local raycastinstance = raycastresult.Instance
if raycastinstance:IsDescendantOf(character) then
return true
end
else
return false
end
end
local function Hunt(character)
local char = workspace:WaitForChild(character.Name)
local distance = (rig.HumanoidRootPart.Position - char.HumanoidRootPart.Position).Magnitude
if distance > 7 then
rig.Humanoid:MoveTo(char.HumanoidRootPart.Position)
else
local anim = script.Animation
print("Grabbed!")
hunting = false
char:SetAttribute("Grabbed", true)
getrandomplr()
for _,v in pairs(char:GetChildren()) do
if v:IsA("Part") then
v.CanCollide = false
v.Massless = true
end
end
local track = char.Humanoid:LoadAnimation(anim)
char.HumanoidRootPart.CFrame = rig.HumanoidRootPart.CFrame
local weld = Instance.new("WeldConstraint", rig.HumanoidRootPart)
weld.Part0 = rig["Right Arm"]
weld.Part1 = char.HumanoidRootPart
weld.Name = "GrabWeld"
track:Play()
end
end
local function calculatepath(destination)
local agentparams = {
['AgentHeight'] = 6,
['AgentRadius'] = 4,
['AgentCanJump'] = true
}
local path = pfs:CreatePath(agentparams)
path:ComputeAsync(rig.HumanoidRootPart.Position, destination)
return path
end
local function walktodestination(destination)
local path = calculatepath(destination)
if path.Status == Enum.PathStatus.Success then
for _, waypoint in pairs(path:GetWaypoints()) do
local huntedplayer = randomplr
if huntedplayer and hunting == true then
sound:Play()
if waypoint.Action == Enum.PathWaypointAction.Jump then
print('jump')
workspace.Dummy.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Hunt(huntedplayer)
break
else
rig.Humanoid:MoveTo(waypoint.Position)
rig.Humanoid.MoveToFinished:Wait()
end
end
else
rig.Humanoid:MoveTo(destination - (rig.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomwaypoint = waypoints[math.random(1, #waypoints)]
walktodestination(randomwaypoint.Position)
end
while task.wait(0.3) do
local ran = math.random(1,5)
if ran == 5 then
hunting = true
print("Hunting")
end
patrol()
end