player:Move() doesn't work properly

So I want to make the player move left and right with the player:Move() function but it doesn’t work properly

If I go either left or right and then right after move to the opposite direction, the player moves only for one second and then stops moving even I’m holding the button to move.

I tried searching on the devforum about someone that had the same issue as me but I couldn’t find any similar topic. So that’s why I made one.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.D then
		player:Move(Vector3.new(1, 0, 0), true)
	elseif input.KeyCode == Enum.KeyCode.A then	
		player:Move(Vector3.new(-1, 0, 0), true)
	elseif input.KeyCode == Enum.KeyCode.W then
		local hum = player.Character.Humanoid
		hum:ChangeState(Enum.HumanoidStateType.Jumping)

	end
end)

uis.InputEnded:Connect(function(input)
	player:Move(Vector3.new(0, 0, 0), true)
end)

robloxapp-20221030-1614343.wmv (591.3 KB)

In the video, I’m not spamming left and right, I’m holding left and right. But even If I hold, the player just moves for 1 second and then stops.

3 Likes

You need to loop the movement as it is firing only once when you hold it.

3 Likes

So, how should I do that? Can you show me any reference script?

2 Likes
3 Likes

It says keydown is not a valid member of “Enum”

1 Like

It is used like this:

UserInputService:IsKeyDown(Enum.KeyCode.E)

For the loop, I’m going to write one in here quickly, give me a sec.

Example script:

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer

while RunService.Heartbeat:Wait() do
	player:Move(Vector3.new(0, 0, 0), true)
	if UserInputService:IsKeyDown(Enum.KeyCode.A) then
		player:Move(Vector3.new(-1, 0, 0), true)
	elseif UserInputService:IsKeyDown(Enum.KeyCode.D) then
		player:Move(Vector3.new(1, 0, 0), true)
	elseif UserInputService:IsKeyDown(Enum.KeyCode.W) then
		local hum = player.Character.Humanoid
		hum:ChangeState(Enum.HumanoidStateType.Jumping)
	end
end

This should simulate the same movement as roblox but without forward and backwards.

But if you test this you will see you can fly just by holding W. To prevent that, use this one.

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer

local JumpDB = true
UserInputService.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.KeyCode == Enum.KeyCode.W and JumpDB then
		JumpDB = false
		local hum = player.Character.Humanoid
		hum:ChangeState(Enum.HumanoidStateType.Jumping)
		task.wait(0.5)
		JumpDB = true
	end
end)

while RunService.Heartbeat:Wait() do
	player:Move(Vector3.new(0, 0, 0), true)
	if UserInputService:IsKeyDown(Enum.KeyCode.A) then
		player:Move(Vector3.new(-1, 0, 0), true)
	elseif UserInputService:IsKeyDown(Enum.KeyCode.D) then
		player:Move(Vector3.new(1, 0, 0), true)
	end
end
2 Likes

i think i had the same issue as you when i was a noob making my own walk script.

the only difference is that i used contextActionService while you used userInputService

the reason why your code is not working is because you’ve told the game “if input ended, stop the character”

which just means you can be pressing any of the keys (wasd) and if you lift one key up your entire character stops moving

this was the exact same problem i had when i was making my own custom walk script

i ended up using roblox’s method as it was the most simple but i didnt really realize i was using roblox’s method of making the character move until i could read code well enough that i decided to challenge myself to read roblox’s walkScript which was placed in the playerModule to see if i could understand what was written

so the main reason why your code isnt working to sum it up is because you’ve told the script to stop moving the character if ANY KEYS IS LIFTED

so kind of like this
you: “press W and expects the character to move forward”
game: move character forward
you: “lift M key and expects the character to keep moving forward”
game: input ended event triggered, blindly stop the character
you: “im still holding the W key. I expected the character to keep moving forward”

if you put some if statements on your inputEnded event, you should be fine

and also you should use runService to update player:Move

local w,a,s,d = 0,0,0,0
local runService = game:GetService("RunService")

runService:BindToRenderStep(function() --do not write bind to render step like this, you'll have to fill in a couple of arugments. Im just simplifying it
    local moveDirection = Vector3.new(d - a, 0, s - w)
    player:Move(moveDirection, true)
end)

you need run service to update the player’s movement because what will happen if you dont is that if you hold down W then turn the camera to the left, your character wont be walking where the camera is looking at

and also the documentation for bindToRenderStep because i’ve simplified the process of writing it in the code above RunService | Roblox Creator Documentation
this documentation will show you the proper way

i just realized you look like your making a movement script for a 2d game and you might not actually need runService

basically to sum this up, the line of problem is this 3 lines of code

uis.InputEnded:Connect(function(input)
	player:Move(Vector3.new(0, 0, 0), true) --put your if statements and your fine to go
end)

code improvement suggestion: you dont need to change the state of the humanoid
you can just do “humanoid.Jump = true”
https://create.roblox.com/docs/reference/engine/classes/Humanoid#Jump
just a tiny quality of life change that improves the understandability of the logic in your code unless making the humanoid jump isnt what you want

any questions, just ask

3 Likes

i dont think were suppose to write the whole script for him

@dthecoolest
i dont think his going to understand whats going on in that post and i dont think the solution in that post helps him makes his 2d game considering player:Move() did its job and he isnt even aware that his code logic is causing the problem here

3 Likes

So there is something better than player:Move()?

1 Like

Also I’m so sorry for not answering fast. We probably have different timezones and I went to sleep.

1 Like

Thanks didn’t consider the 3 lines of code causing the issue, always just assumed player:Move or humanoid:Move needs to be looped in order to work.

However for @Raven_fortnite3 for a question like that even if it’s a sub question from the main topic.

You should search it up first which is what I did for you if you wanted to know how to loop the movement.

no, player:Move is your best bet.

1 Like

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