Whats wrong with my script?

Hello! I am currently trying to implement an eight way walk system into a script, however for some reason “ForwardRight” cannot be detected and won’t be printed. Can someone help me out?

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRoot = Character:WaitForChild("HumanoidRootPart")
-----AnimationHandler
local Animate = script
local RelativeWalk = script:WaitForChild("RelativeWalk")
local animback = Humanoid:LoadAnimation(script.RelativeWalk:FindFirstChild("WalkAnimBack"))
local animfront = Humanoid:LoadAnimation(script.RelativeWalk:FindFirstChild("WalkAnimFront"))
local animleft = Humanoid:LoadAnimation(script.RelativeWalk:FindFirstChild("WalkAnimLeft"))
local animright = Humanoid:LoadAnimation(script.RelativeWalk:FindFirstChild("WalkAnimRight"))
local animidle = Humanoid:LoadAnimation(script.RelativeWalk:FindFirstChild("WalkAnimIdle"))
---Animations' Names
local WalkF = RelativeWalk.WalkAnimFront
local WalkB = RelativeWalk.WalkAnimBack
local WalkR = RelativeWalk.WalkAnimRight
local WalkL = RelativeWalk.WalkAnimLeft
-----Varibles
local RunSer = game:GetService("RunService")
--(*)
local CurrentAnim = nil 

-----Functions^^}

function ToWalkRelativeToHRP(Humanoid, Direction, RootCompare)

	-----Check if player is moving
	if Direction ~= nil then
		----X-x-x Continue

		local RootFacing = RootCompare.CFrame.LookVector
		local RootRight = RootCompare.CFrame.RightVector

		local DirectionRelativeToRootFront = RootFacing:Dot(Direction, RootFacing)
		local DirectionRelativeToRootSides = RootRight:Dot(Direction, RootRight)
		----

		local Output = nil

		if DirectionRelativeToRootFront <= 1 and DirectionRelativeToRootFront >= 0.75 then
			Output = "Forwards"
		elseif DirectionRelativeToRootFront >= -1 and DirectionRelativeToRootFront <= -0.75 then
			Output = "Backwards"
		elseif DirectionRelativeToRootSides <= 1 and DirectionRelativeToRootSides >= 0.75 then
			Output = "Right"
		elseif DirectionRelativeToRootSides >= -1 and DirectionRelativeToRootSides <= -0.75 then
			Output = "Left"
		elseif DirectionRelativeToRootFront <= 1 and DirectionRelativeToRootSides >= 0.75 then
			Output = "ForwardsRight" -- cannot be detected for some reason
		end

		return Output
	end

end

----------Below heres is not needed but just shows the ways you could use it!
RunSer.RenderStepped:Connect(function()
	local ForceVector = ToWalkRelativeToHRP(Humanoid, Humanoid.MoveDirection, HumanoidRoot)
	print(ForceVector)

	if ForceVector == "Forwards" then
		script.WalkDirection.Value = "Forward"
	elseif ForceVector == "Backwards" then
		script.WalkDirection.Value = "Backward"
	elseif ForceVector == "Right" then
		script.WalkDirection.Value = "Right"
	elseif ForceVector == "Left" then
		script.WalkDirection.Value = "Left"
	elseif ForceVector == nil then
		script.WalkDirection.Value = "Idle"
	elseif ForceVector == "ForwardRight" then
		script.WalkDirection.Value = "ForwardRight"
	end

end)

Have you tried printing what DirectionRelativeToRootFront and DirectionRelativeToRootSides are when it fails? Just add an “else” and then print them. If they don’t print, then one of the other if/elseifs is passing. If they print, see what they are.
As it stands, I have a hunch that it may be related to a diagonal unit vector only being 0.707 in each direction.

1 Like

Nevermind, turns out it was the 7.07 thing. thanks!

1 Like