Game is not detecting when the player isn't Anchored properly

I have a Script for an NPC that grabs the player and waits a certain amount of time them detects if they are anchored or not, when the player is not anchored it still thinks that the player is anchored, this code:

local TS = game:GetService("TweenService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local animation = script:WaitForChild("Animation")
local animtrack = humanoid:LoadAnimation(animation)
local animation3 = script:WaitForChild("Animation3")
local animtrack3 = humanoid:LoadAnimation(animation3)
local animplaying = false
local zomtor = script.Parent.Torso


function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 5
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) and (temp2.Name ~= "Zombie") and (temp2.Name ~= "Ragdoll") and (temp2.Name ~= "Rig") then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = human
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

while true do
	wait(0.01)
	target = findNearestTorso(script.Parent.Torso.Position)
	if target ~= nil then
		local animation2 = script:WaitForChild("Animation2")
		local animtrack2 = target:LoadAnimation(animation2)
		local animation4 = script:WaitForChild("Animation4")
		local animtrack4 = target:LoadAnimation(animation4)
		
		if animplaying == false then
			local Weld = Instance.new("Weld")
			
			target.WalkSpeed = 0
			animtrack:Play()
			animplaying = true
			
			target.Parent:findFirstChild("HumanoidRootPart").CFrame = CFrame.new(target.Parent:findFirstChild("HumanoidRootPart").CFrame.Position, zomtor.Position) * CFrame.Angles(0,math.rad(180),0)
			target.Parent:findFirstChild("HumanoidRootPart").Anchored = true -- turning the player away from the NPC since they are being grabbed from behind and then anchoring them
			
			wait(0.3)
			
			animtrack2:Play()
			
			local orientation = zomtor.Orientation
			Weld.Parent = zomtor
			Weld.Part0 = zomtor
			Weld.Part1 = target.Parent:findFirstChild("HumanoidRootPart")
			local WeldingCfr = zomtor.CFrame
			local ObjectCframe = zomtor.CFrame:ToObjectSpace(WeldingCfr):Inverse()
			Weld.C1 = ObjectCframe * CFrame.Angles(0,math.rad(5),0) + Vector3.new(0,-0.5,1.2) -- welds me to the torso of the NPC
			
			wait(0.1)
			
			animtrack3:Play()
			animtrack4:Play()
			
			wait(3)
			
			humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false) -- NPC kept falling down so this is to stop that
			animtrack4:Stop()
			Weld:Destroy() -- unweld the player so that they can go away or die
			target.WalkSpeed = 16 -- setting walkspeed back to normal since it was only set to zero so the player doesnt run off while being grabbed
			
			if target.Parent:findFirstChild("HumanoidRootPart").Anchored == true then
				print(target.Parent:findFirstChild("HumanoidRootPart").Anchored)
				wait(0.01)
				target.Health = 0 -- you must die >:3
			end
			target.Parent:findFirstChild("HumanoidRootPart").Anchored = false
			
			animtrack3:Stop()
			
			wait(1)
			
			humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, true) -- lets the NPC fall down again since the grabbing is done
			animplaying = false
		end
	end
end

This is what makes the player elbow the NPC, and makes them go unanchored

local UserInputService = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local root = character:WaitForChild("HumanoidRootPart")
local camera = game.Workspace.CurrentCamera
local fovval = camera.FieldOfView
local animation = script:WaitForChild("Animation")
local humanoid = script.Parent:WaitForChild("Humanoid")
local animtrack = humanoid:LoadAnimation(animation)
local hitnum = 0
local cooldown = false


UserInputService.InputBegan:Connect(function(input, gameProcessed)

	if input.KeyCode == Enum.KeyCode.E and root.Anchored == true and cooldown == false then -- if you press E and are anchored and arnt on cool down, you will elbow the NPC
		animtrack:Play()
		cooldown = true -- putting you on cooldown so you cannot elbow again while you are in the act of elbow-ing
		
		if hitnum < 2 then
			hitnum = hitnum + 1 -- every time you elbow the NPC, hitnum goes up unless you have hit them 2 or more times
		end
		
		if hitnum > 1 then
			print(hitnum)
			hitnum = 0
			root.Anchored = false -- if you have hit the NPC two or more times, print the amount of times hit then reset the hit counter, and unanchor the player so that the NPC can detect that you have succeded the fightback check
		end
		
		wait(0.9)
		cooldown = false -- when you loop through the code, it waits 0.9 seconds before setting cooldown to false
	end
end)

please tell me why its not working, even when i can tell im not anchored because i can move while im being grabbed it still kills me, if anyone has the answer please give it to me!

2 Likes

To answer my own question, apparently there is a difference between Local unanchoring and Serverside unanchoring, even though i would assume to not be anchored on the Clientside you would not be able to be anchored still Serverside, but uh… yeah… anyone who wanted the answer, use remote events.

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