Spherecast not casting sometimes, Spherecast.Instance never getting detected despite conditions being fully met

Hello, so i’ve made a WallJump function. Simply put, when the Player jumps/falls, a Spherecast is casted. If the Spherecast exists, then it prints “Casted Sphere.”. Then, if the Spherecast hits a specific WallJump part, then the HumanoidRootPart of the Player gets anchored, and it prints “Hit JumpPanel.”. Or at least, that’s what it’s supposed to do. I do not know or understand why, but the Spherecast here is really inconsistent. If the Player jumps or falls, then the Spherecast will randomly cast. And even then, sometimes it doesn’t do it. AND, if it is touching the WallJump part, it just refuses to print “Hit Jump Panel.” and anchor the HumanoidRootPart, as it’s supposed to. I’ve been trying so hard to fix this, and i even tried raycasting instead, but it had the same exact same issues. Please help, this is extremely frustrating.

local function WallJump()
    local Params = RaycastParams.new()
    Params.FilterType = Enum.RaycastFilterType.Exclude
    Params.FilterDescendantsInstances = {Character}
    --------------------------------------
    local WJAnim = Animator:LoadAnimation(Animations.HoldWall)
    --------------------------------------
    local Spherecast = workspace:Spherecast(Root.Position, 5, Root.CFrame.LookVector * 1, Params)
    --------------------------------------
    if Spherecast then
        print("Casted Sphere.")
        if Spherecast.Instance and Spherecast.Instance.Name == "Hitbox" and Spherecast.Instance.Parent.Name == "JumpPanel" then
            print("Hit JumpPanel.")
            Root.Anchored = true
            Root.CFrame = CFrame.new(Spherecast.Position, Spherecast.Position + Spherecast.Normal)
        end
    end
end
local function StateHandler(Old, New)
    if New == Enum.HumanoidStateType.Jumping or New == Enum.HumanoidStateType.Freefall then
        --[[spawn(function()
            if Bools.WallSpotted.Value == false then
                task.wait(0.2)
                Bools.CanDJ.Value = true
            end
        end)]]
        WallJump()
    elseif New == Enum.HumanoidStateType.Landed then
        --Bools.CanDJ.Value = false
        --Bools.HasDJ.Value = false
    end
end

local function WallJump()
    local Params = RaycastParams.new()
    Params.FilterType = Enum.RaycastFilterType.Exclude
    Params.FilterDescendantsInstances = {Character}
    --------------------------------------
    local Spherecast = workspace:Spherecast(Root.Position, 5, Root.CFrame.LookVector * 5, Params)

    if Spherecast then
        print("Casted Sphere.", Spherecast.Instance, Spherecast.Instance.Parent)

        if Spherecast.Instance and Spherecast.Instance.Parent then
            if Spherecast.Instance.Name == "Hitbox" and Spherecast.Instance.Parent.Name == "JumpPanel" then
                print("Hit JumpPanel.")
                Root.Anchored = true
                Root.CFrame = CFrame.new(Spherecast.Position, Spherecast.Position + Spherecast.Normal)
            end
        end
    end
end
local function StateHandler(Old, New)
    if New == Enum.HumanoidStateType.Jumping or New == Enum.HumanoidStateType.Freefall then
        game:GetService("RunService").Heartbeat:Connect(WallJump) 
    end
end

It still refuses to work, and doesn’t print anything at all. Thanks for your help tho.

1 Like

Nevermind, i ended up using a while Humanoid:GetState() == Enum.HumanoidStateType.Jumping or Humanoid:GetState() == Enum.HumanoidStateType.Freefall do loop which ended up fixing the issue.

1 Like

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