How to create a walking detector? (I elaborate in the desc)

If you’ve seen my previous post, welcome back.

Let me elaborate. My gun uses math to create a lissajous curve. And I now need to create Lerping movement when the player stops moving, and detection to see when the player walks so it moves when you walk.

So far, I haven’t got it to work yet. Here is my code:

local runservice = game:GetService("RunService")
local RepFirst = game.ReplicatedFirst

local Cam = workspace.CurrentCamera
local CamPos = Cam.CFrame

local Plr = game.Players.LocalPlayer
local Chr = Plr.CharacterAdded:Wait()

local Model = RepFirst.Luger
local Tool = script.Parent

local isRunning = false

Tool.Equipped:Connect(function()
	Luger = Model:Clone()
	Luger.Parent = workspace
	
	local GunPos = Luger.CameraBone
	
	local a = 0
	local speed = 2
	local radius = 0.4
	
	while true do
		wait()
		if not game.Players.LocalPlayer.Character:WaitForChild("Humanoid").MoveDirection == Vector3.new(0, 0, 0) then
			if not isRunning then
				print('walking')
				isRunning = true
				game:GetService("RunService").RenderStepped:Connect(function(dt)
					a += dt*speed

					GunPos.CFrame = CFrame.new(Cam.CFrame.Position)

					local X, Y = radius*math.cos(a), radius*math.sin(2*a)
					GunPos.CFrame = Cam.CFrame * CFrame.new(X,Y,0)

				end)
			end
		else
			isRunning = false
		end
	end

end)

Tool.Unequipped:Connect(function()
	wait(1)
	Luger:Destroy()
end)

The movement works fine. I can’t figure out the lerping, and I can’t get the detection to work. Anything helps.

You can use CFrame:Lerp(newCF, alpha) or Vector3:Lerp() to lerp.

As for detection when the player walks, you can check if Humanoid.MoveDirection.Magnitude>0 from a LocalScript.

Edit: You need to put the move direction statement inside of the loop.

local runservice = game:GetService("RunService")
local RepFirst = game.ReplicatedFirst

local Cam = workspace.CurrentCamera
local CamPos = Cam.CFrame

local Plr = game.Players.LocalPlayer
local Chr = Plr.CharacterAdded:Wait()
local Hum = Chr:WaitForChild("Humanoid")

local Model = RepFirst.Luger
local Tool = script.Parent

local isRunning = false

Tool.Equipped:Connect(function()
	Luger = Model:Clone()
	Luger.Parent = workspace
	
	local GunPos = Luger.CameraBone
	
	local a = 0
	local speed = 2
	local radius = 0.4
	
	while true do
		wait()
		if Hum.MoveDirection.Magnitude>0 then
			if not isRunning then
				print('walking')
				isRunning = true
				running = game:GetService("RunService").RenderStepped:Connect(function(dt)
					a += dt*speed

					GunPos.CFrame = CFrame.new(Cam.CFrame.Position)

					local X, Y = radius*math.cos(a), radius*math.sin(2*a)
					GunPos.CFrame = Cam.CFrame * CFrame.new(X,Y,0)

				end)
			end
		else
			isRunning = false
			if running then
				running:Disconnect()
				GunPos.CFrame = CFrame.new(Cam.CFrame.Position)
			else
				return
			end
		end
	end

end)

Tool.Unequipped:Connect(function()
	wait(1)
	Luger:Destroy()
end)

Doesn’t seem to work. Anything I’m doing wrong?

You never said isRunning = true if the move direction is greater than 0

1 Like

Just fixed it. The model wont go to the camera position unless I equip it.

local runservice = game:GetService("RunService")
local RepFirst = game.ReplicatedFirst

local Cam = workspace.CurrentCamera
local CamPos = Cam.CFrame

local Plr = game.Players.LocalPlayer
local Chr = Plr.CharacterAdded:Wait()
local Hum = Chr:WaitForChild("Humanoid")

local Model = RepFirst.Luger
local Tool = script.Parent

local isRunning = false

Tool.Equipped:Connect(function()
	Luger = Model:Clone()
	Luger.Parent = workspace
	
	local GunPos = Luger.CameraBone
	
	GunPos.CFrame = CFrame.new(Cam.CFrame.Position)
	
	local a = 0
	local speed = 2
	local radius = 0.4
	
	while true do
	wait()
	if Hum.MoveDirection.Magnitude>0 then
		isRunning = true
			print('walking')
			running = game:GetService("RunService").RenderStepped:Connect(function(dt)
				a += dt*speed

				GunPos.CFrame = CFrame.new(Cam.CFrame.Position)

				local X, Y = radius*math.cos(a), radius*math.sin(2*a)
				GunPos.CFrame = Cam.CFrame * CFrame.new(X,Y,0)
			end)
		else
			isRunning = false
			if running then
				running:Disconnect()
				GunPos.CFrame = CFrame.new(Cam.CFrame.Position)
			else
				return
			end
		end
	end

end)

Tool.Unequipped:Connect(function()
	wait(1)
	Luger:Destroy()
end)