Not sure how to fix bug with player when welded to another humanoid

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

There have been a lot of posts about this already.
When you weld one player or NPC to another the physics of both Humanoids fight each other.

Try searching ‘weld players together’ or ‘npc welded to player’ or ‘carry other players’ to see if you get answers from those search results.

I’ve searched a bit and I don’t see any fixes for this. Maybe there’s a way to force the players humanoid to stop from trying to fight for control?

Try adding:

char.HumanoidRootPart:SetNetworkOwner(nil)
char.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)

after you weld the characters together
and to revert it, do:

local player = players:GetPlayerFromCharacter(char)
if player then
	if char.HumanoidRootPart:CanSetNetworkOwnership() then
		char.HumanoidRootPart:SetNetworkOwner(player)
	end
end
		
char.Humanoid:ChangeState(Enum.HumanoidStateType.Running)

let me know if it’s still happening or not after trying this.

2 Likes

this worked perfectly. I just need to add some finishing touches and it will be perfect! Thank you!

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