I want to create a monster that grabs a random player and takes them to a cage where they trap them inside before going back out and continuing the loop.
The monster breaks after I use the touch event to see if the monster has reached the destination to drop the player in the cage.
(Problem occurs in the “Code” section)
-- 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
local grabsfx = rig["Right Arm"]["Grab Sound"]
local grabsfx2 = rig["Right Arm"]["Grabbed start"]
local location = game.Workspace:WaitForChild("MonsterTake")
-- states
local hunting = false
local holding = 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 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 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 > 6 then
local destination = char.HumanoidRootPart.Position
local path = calculatepath(destination)
for _, waypoint in pairs(path:GetWaypoints()) do
local huntedplayer = randomplr
if huntedplayer and hunting == true then
if waypoint.Action == Enum.PathWaypointAction.Jump then
print('jump')
rig.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
rig.Humanoid:MoveTo(waypoint.Position)
end
end
else
local anim = script.Animation
print("Grabbed!")
grabsfx:Play()
grabsfx2:Play()
holding = true
hunting = false
char:SetAttribute("Grabbed", true)
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"
char.HumanoidRootPart:SetNetworkOwner(nil)
char.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
track:Play()
end
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()
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
-- code
rig["Left Leg"].Touched:Connect(function(hit)
if hit == location and holding == true then
print("Dropped!")
holding = false
local char = workspace:WaitForChild(randomplr.Name)
local anim = script.Animation
local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
local captured = game.Workspace.Captured
hrp.CFrame = char:FindFirstChild("Torso").CFrame
for i,v in pairs(hum:GetPlayingAnimationTracks()) do
v:Stop()
end
char:SetAttribute("Grabbed", false)
hrp:SetNetworkOwner(players:GetPlayerFromCharacter(char))
hum:ChangeState(Enum.HumanoidStateType.GettingUp)
local weld = rig.HumanoidRootPart:WaitForChild("GrabWeld")
if weld then
weld:Destroy()
end
for _,v in pairs(char:GetChildren()) do
if v:IsA("Part") then
v.CanCollide = true
v.Massless = false
end
end
task.wait(1)
hrp.CFrame = captured.CFrame
getrandomplr()
end
end)
while task.wait() do
local ran = math.random(1,5)
if ran == 5 and hunting == false then
hunting = true
print("Hunting")
end
if holding == false then
patrol()
else
walktodestination(location.CFrame.Position)
end
end