Movement Detection Script Randomly Stopped Working

I was attempting to make some code that detects movement on all platforms, wether it be virtual or physical joystick input, or keyboard. I got it working perfectly fine, but it randomly decided to stop detecting even after like 3 tests with the same results. Im genuinely confused on why it would do this, and wether its a studio glitch or something I did to somehow affect the way the code operated, because I didn’t change anything about it between the time I tested it working, and when I tested it to not work. To specify, the glitch transferred to the actual game on the Roblox app, so idk.

Here’s the script, there may be some unrelated stuff, but the detection is fully inside of this script (mainly within the first RenderStepped function).

local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local RunService = game:GetService("RunService")

local Control = require(Player.PlayerScripts.PlayerModule.ControlModule)

RunService.RenderStepped:Connect(function()
	if script.Parent.InWater.Value == false then
		local InputDirection = Control:GetMoveVector()
		if InputDirection.Magnitude > 0 then
			if InputDirection.Z < -0.5 and InputDirection.Z > -1 then
				local X, Y, Z = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
				script.Parent.MoveForwards:FireServer(X, Y, Z) if game.SoundService.Swim2.Playing == false then game.SoundService.Swim2:Play() end
			elseif InputDirection.Z > 0.5 and InputDirection.Z < 1 then
				local X, Y, Z = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
				script.Parent.MoveBackwards:FireServer(X, Y, Z) if game.SoundService.Swim2.Playing == false then game.SoundService.Swim2:Play() end
			end
		end
	end
end)

game:GetService("RunService").RenderStepped:Connect(function(deltaTime: number)
	if script.Parent.InWater.Value == true then
		if script.Parent.Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
			if game.SoundService.Swim3.Playing == false then game.SoundService.Swim3:Play() end
		end
	end
end)

The way I detect the movement works fine if I just do InputDirection == -1 or positive 1, but then its to precise to detect for mobile or console. I even tried testing on the previous version that was working, that for some reason stopped working too, which gives me even more of an inclination that this is a studio issue.

1 Like

Is there a reason you’re using both the player control module and Humanoid.MoveDirection?

1 Like

Did you fork PlayerScripts? Seems to me that Roblox might’ve shipped an update to their ControlModule or something along those lines

1 Like

The move direction is related to something else (the thing in the second render stepped thing) as for what paracosm said. Can you tell me what the update was, and how I can adapt my code?

1 Like

I imagine you’re trying to determine if the player is moving forwards, but you can also use MoveDirection for that instead of the ControlModule, you just need to use the character’s root part CFrame:

local function getMoveDirection(character : Model) : Vector3
    local moveDirection = character.Humanoid.MoveDirection
    local rootCFrame = character.HumanoidRootPart.CFrame
    return rootCFrame:VectorToObjectSpace(moveDirection)
end

Also about precision, you don’t need to check if any of the directional components are less than 1, you can just use the 0.5 inequality. If you must, change the > -1 and < 1 to >= -1 and <= 1, as sometimes the move direction components can be 1.

local x = 1
print(x > x) --> false
print(x < x) --> false
print(x >= x) --> true
print(x <= x) --> true
1 Like

For some reason the second part of the if statements made it break, most likely because -1 or 1 would be the maximum amount for those directions, and therefore cannot be larger or more in the negative. It works now.

2 Likes

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