RaycastResult variable not updating in renderstepped

Hi,

I am having weird issues with my raycasts and for some reason they are not updating since the first raycast. For example, when I first detect a wall with a ray, it works fine, but when the i am outside of the rays perimeter, it still thinks that its detecting the wall.

For example in my wall run function, when I wall run it works fine but when the wall ends and the ray = nil, it still thinks that its detecting the wall.

Here’s the wall run function:


function Movement.Wallrun(self: ClassType, Humanoid, Root)
	local isGrounded = if Humanoid.FloorMaterial == Enum.Material.Air then false else true
	local velocity = Root.AssemblyLinearVelocity.Magnitude
	
	local LeftWallCheck = WallrunFuncs[1](Root, PlayerSettings.WallrunRayLength,self.wallrunParameters) :: RaycastResult
	local RightWallCheck = WallrunFuncs[2](Root, PlayerSettings.WallrunRayLength,self.wallrunParameters) :: RaycastResult
	local FloorCheck = WallrunFuncs[3](Root, PlayerSettings.WallrunFloorRayLength, self.wallrunParameters) :: RaycastResult

	if not LeftWallCheck and not RightWallCheck then return end 

	if self.previousWall ~= nil then
		local Result = LeftWallCheck or RightWallCheck
		if self.previousWall == Result.Instance and Humanoid:GetState() == Enum.HumanoidStateType.Freefall or not isGrounded then
			print("Player tried to wallrun while freefalling on previous wall")
			return
		end
	end
	
	task.spawn(function()
		WallrunUpdate = RunService.RenderStepped:Connect(function(deltaTime: number) 
			local Result = LeftWallCheck or RightWallCheck

			if Result and velocity >= 21 then
				self.isWallrunning = true

				WallrunFuncs[4](Result.Position)

				Root.AssemblyLinearVelocity = Vector3.new(Root.AssemblyLinearVelocity.X,-1,Root.AssemblyLinearVelocity.Z)

				self.previousWall = Result.Instance
			elseif not Result or velocity < 21 or Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
				self.isWallrunning = false
				
				if WallrunUpdate then
					WallrunUpdate:Disconnect()
				end
			end
		end)
	end)
end
1 Like

you need to perform the raycasting within the RenderStepped loop so that it continuously checks for walls as the player moves. Here’s a revised version of your Wallrun function:

function Movement.Wallrun(self: ClassType, Humanoid, Root)
	local function isGrounded()
		return Humanoid.FloorMaterial ~= Enum.Material.Air
	end

	local function getVelocity()
		return Root.AssemblyLinearVelocity.Magnitude
	end

	if self.previousWall ~= nil then
		local LeftWallCheck = WallrunFuncs[1](Root, PlayerSettings.WallrunRayLength, self.wallrunParameters) :: RaycastResult
		local RightWallCheck = WallrunFuncs[2](Root, PlayerSettings.WallrunRayLength, self.wallrunParameters) :: RaycastResult

		local Result = LeftWallCheck or RightWallCheck
		if self.previousWall == Result.Instance and Humanoid:GetState() == Enum.HumanoidStateType.Freefall or not isGrounded() then
			print("Player tried to wallrun while freefalling on previous wall")
			return
		end
	end
	
	task.spawn(function()
		WallrunUpdate = RunService.RenderStepped:Connect(function(deltaTime: number) 
			local LeftWallCheck = WallrunFuncs[1](Root, PlayerSettings.WallrunRayLength, self.wallrunParameters) :: RaycastResult
			local RightWallCheck = WallrunFuncs[2](Root, PlayerSettings.WallrunRayLength, self.wallrunParameters) :: RaycastResult
			local FloorCheck = WallrunFuncs[3](Root, PlayerSettings.WallrunFloorRayLength, self.wallrunParameters) :: RaycastResult

			local Result = LeftWallCheck or RightWallCheck
			local velocity = getVelocity()

			if Result and velocity >= 21 then
				self.isWallrunning = true

				WallrunFuncs[4](Result.Position)

				Root.AssemblyLinearVelocity = Vector3.new(Root.AssemblyLinearVelocity.X, -1, Root.AssemblyLinearVelocity.Z)

				self.previousWall = Result.Instance
			elseif not Result or velocity < 21 or Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
				self.isWallrunning = false
				
				if WallrunUpdate then
					WallrunUpdate:Disconnect()
				end
			end
		end)
	end)
end
2 Likes

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