If statement isn't working?

basically, I have an npc of which if the NPC is more than 7 (in magnitude) away from you, the NPC will walk towards you, if magnitude is less then 7, then the NPC will stop as it is “Too Close”, the problem is though, the other if statement is if the magnitude is greater than 20, the NPC should change its walkspeed to 20, and a sprinting animation should play (as the one seen in the video), however it doesnt work, no errors either.

VIDEO:

CODE:

local Char = script.Parent
local Humanoid = Char:FindFirstChild("Humanoid")
local HumanoidRootPart = Char:WaitForChild("HumanoidRootPart")
local Head = Char:WaitForChild("Head")
local Walk = Humanoid.Animator:LoadAnimation(Char.Walk)
local Run = Humanoid.Animator:LoadAnimation(Char.Animation)
local Idle = Humanoid.Animator:LoadAnimation(Char.Idle)
local RunService = game:GetService("RunService")

--local Jump = Humanoid.Animator:LoadAnimation(Char.Jump)

Char.PrimaryPart:SetNetworkOwner(nil)


local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	
	return nearestTarget
end

Humanoid.Running:Connect(function(speed)
	--print("NPC Speed is :" ..speed)
	if speed > 0 then
		--Walk
		Idle:Stop()
		Walk:Play()
	elseif speed > 17 then
		--Run
		print("Should Be Running")
		Idle:Stop()
		Walk:Stop()
		Run:Play()
	else
		--Stop
		Walk:Stop()
		Run:Stop()
		Idle:Play()
	end
end)

local PathfindingService = game:GetService("PathfindingService")

local function getPath(destination)
local path = PathfindingService:CreatePath()

path:ComputeAsync(HumanoidRootPart.Position, destination.Position)
	
	return path
end

local function attack(target)
	local distance = (HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	if distance > 7 then
		print("NPC is Walking")
		Humanoid.WalkSpeed = 6
		Humanoid:MoveTo(target.HumanoidRootPart.Position)
	elseif distance > 20 then
		print("Distance is greater than 20; Should be Running")
		Humanoid.WalkSpeed = 20
	elseif distance < 7 then
		print("Too Close, NPC Stopped")
		Humanoid.WalkSpeed = 0
	end
	
	
	--[[
	if distance > 4 then --not close enough
		Humanoid:MoveTo(target.HumanoidRootPart.Position)
		Humanoid.WalkSpeed = 6
	else --close
		Humanoid.WalkSpeed = 0

	end
	-]]
end

local function walkTo(destination)
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target then
				--print("TARGET FOUND",target.Name)
				attack(target)
				break
				else
					--print("Moving to", waypoint.Position)
			Humanoid:MoveTo(waypoint.Position)
				Humanoid.MoveToFinished:Wait()
			end
		end
	else
		Humanoid:MoveTo(destination.Position - (HumanoidRootPart.CFrame.LookVector * 10)) --NPC walk back if hit wall
	end
end



local function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])

end


while task.wait(.5) do

	patrol()
end

if distance > 20 then
	print("Distance is greater than 20; Should be Running")
	Humanoid.WalkSpeed = 20
elseif distance > 7 then
	print("NPC is Walking")
	Humanoid.WalkSpeed = 6
	Humanoid:MoveTo(target.HumanoidRootPart.Position)
else
	print("Too Close, NPC Stopped")
	Humanoid.WalkSpeed = 0
end

I think since its gonna be bigger than 7 before its bigger than 20 that the 20 never gets ran, try this

NPC is really awkward, at first it doesnt move unless you get in the distance, while in the original video, it followed instantly, also in the Humanoid.Running event, the sprinting anim still doesnt play even if its in the speed.

because you arent calling MoveTo in your running code

this seems better for your use:

local funtion UpdateMovementState()
	local WS = 0
	if distance > 7 then
		WS = 6
	end
	if distance > 20 then
		WS = 20
	end
	Humanoid.WalkSpeed = WS
	return WS
end


if UpdateMovementState() > 0 then
	Humanoid:MoveTo(target.HumanoidRootPart.Position)
end

works, but the sprinting animation still doesnt play. this is the Running Function:

Humanoid.Running:Connect(function(speed)
	--print("NPC Speed is :" ..speed)
	if speed > 0 then
		--Walk
		Idle:Stop()
		Walk:Play()
	elseif speed > 17 then
		--Run
		print("Should Be Running")
		Idle:Stop()
		Walk:Stop()
		Run:Play()
	else
		--Stop
		Walk:Stop()
		Run:Stop()
		Idle:Play()
	end
end)
Humanoid.Running:Connect(function(speed)
	--print("NPC Speed is :" ..speed)
	if speed <= 0 then
		Walk:Stop()
		Run:Stop()
		Idle:Play()
		return
	end
	if speed < 17 then -- bigger than 0, smaller than 17
		--Walk
		Idle:Stop()
        Run:Stop()
		Walk:Play()
	else -- bigger or equal to 17
		--Run
		print("Should Be Running")
		Idle:Stop()
		Walk:Stop()
		Run:Play()
	end
end)
1 Like