Touched Event Affecting Function

I have a function that is called by the characters torso being touched - Torso.Touched - I also have a while true loop that prints the output of a boolean called ClimbingWall - It checks for when the player is climbing the wall.

When the player is not climbing the wall, it correctly prints false. But for some reason, whenever the player is climbing the wall, it will bounce between true and false, and the output will look something like this >>> true, false, true, false, true, false, false, true, false

function EnableClimb()
    if not ClimbingWall and Humanoid:GetState() == Enum.HumanoidStateType.Freefall then  
        local raycastParams = RaycastParams.new()
        local raycast = workspace:Raycast(HumRootPart.Position, HumRootPart.CFrame.LookVector * 1.38, raycastParams)
        ClimbingWall = true

        if raycast and raycast.Instance.Size.Y > 5 then
         FocusPart = raycast.Instance
            local firstTick = tick()
            print(raycast.Normal)
            
            if climbSideValue or climbValue or climbPower then
                climbSideValue, climbValue, climbPower = 0, 2, 1
            end

            HumRootPart.AssemblyLinearVelocity = Vector3.new(0,0,0)

            TweenService:Create(Humanoid, TweenInfo.new(0.4, Enum.EasingStyle.Exponential), { CameraOffset = Vector3.new(0,0,0) }):Play()
            TweenService:Create(camera, TweenInfo.new(0.4, Enum.EasingStyle.Exponential), { FieldOfView = 120 }):Play()

            ClimbConnect = RunService.RenderStepped:Connect(function()
                
                if not ClimbingWall then return end
                
                if FocusPart and Head.Position.Y > (FocusPart.Position.Y + FocusPart.Size.Y / 2) + 0.7 then
                    HumRootPart.AssemblyLinearVelocity = Vector3.new(0, 50, 0)

                    if Sprinting == false then
                        Humanoid.WalkSpeed = 16
                    else
                        Humanoid.WalkSpeed = 27
                    end

                    IdleClimbTrack:Stop()
                    MoveClimbTrack:Stop()
                    DownFOVTween:Play()
                    ClimbingWall = false
                    ClimbConnect:Disconnect()

                    return
                end

                Humanoid.WalkSpeed = 0
                local checkRaycast = workspace:Raycast(HumRootPart.Position, HumRootPart.CFrame.LookVector * 1.38, raycastParams)
                
                if not checkRaycast or checkRaycast.Instance ~= FocusPart then
                    print("TOO BAD")
                    DownFOVTween:Play()
                    
                    task.delay(0.1, function()
                        if not workspace:Raycast(HumRootPart.Position, HumRootPart.CFrame.LookVector * 1.38, raycastParams) then
                            ClimbingWall = false
                            ClimbConnect:Disconnect()
                        end
                    end)
                    return
                end

                if CurrentInputW and not MoveClimbTrack.IsPlaying then
                    IdleClimbTrack:Stop()
                    MoveClimbTrack:Play()
                elseif not CurrentInputW then
                    MoveClimbTrack:Stop()
                    IdleClimbTrack:Play()
                end

                HumRootPart.AssemblyLinearVelocity = CFrame.lookAlong(Vector3.zero, checkRaycast.Normal, Vector3.yAxis):VectorToWorldSpace(Vector3.new(climbSideValue, climbValue * climbPower, 0))
            end)

            while task.wait(0.1) and FocusPart and ClimbingWall do

                if tick() - firstTick > 4 then
                    ClimbConnect:Disconnect()
                    MoveClimbTrack:Stop()
                    IdleClimbTrack:Play()
                    ClimbingWall = false
                    break
                end

                climbPower = CurrentInputW and 5 or -1
                climbSideValue = CurrentInputA and 15 or (CurrentInputD and -15 or 0)

            end
        end
    end
end

How could i fix this?

The Raycast may be failing intermittently, maybe consider using a module like ZonePlus?

2 Likes