Need help with player control script

so I want player move right if he hold R on keyboard (Don’t ask me why) but the problem is if player hold R he move right 1 time only but I want it move right as a repetition and stop moving right if he unhold R so please help and this is my script

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local Character = player.CharacterAdded:Wait()

local humanoid = Character:WaitForChild("Humanoid")

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:connect(function(keyCode)
	local HRP = Character:FindFirstChild("HumanoidRootPart")

	if keyCode.keyCode == Enum.KeyCode.R then
		Character:MoveTo(HRP.Position + Vector3.new(0,0,1))
	end
	end)
1 Like

Why not just create another Event that’ll detect when the Player releases the R Key? You could just move them to their current position which would basically make them stop moving

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

local humanoid = Character:WaitForChild("Humanoid")

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:connect(function(keyCode, Chatted)
	
	if Chatted then
		return
	end
	
	local HRP = Character:FindFirstChild("HumanoidRootPart")

	if HRP ~= nil and keyCode.KeyCode == Enum.KeyCode.R then
		Character:MoveTo(HRP.Position + Vector3.new(0,0,1))
	end
end)

UserInputService.InputEnded:Connect(function(keyCode)
	local HRP = Character:FindFirstChild("HumanoidRootPart")

	if HRP ~= nil and keyCode.KeyCode == Enum.KeyCode.R then
		Character:MoveTo(HRP.Position)
	end
end)
1 Like

you could do
EDIT:fixing up code a bit

RunService.RenderStepped:Connect(function()
	local Velocity = Vector3.new(0,0,0)
	if UserInputService:IsKeyDown(Enum.KeyCode.R) then
		Velocity+=Vector3.new(0,0,1)
	end
	--add other keybinds, like for example f for moving up
	if UserInputService:IsKeyDown(Enum.KeyCode.F) then
		Velocity += Vector3.new(0,1,0)
	end
	Character:Move(Velocity)
end)

Move moves in a direction instead of moving to a point

1 Like

no i want player move right as a repetition if he hold R and stop moving if he unhold R so the problem in the script is if player hold R he move right 1 time only so i want it to move as a repetition and stop moving right if he unhold R i hope you understand

1 Like

look I want player moving right if he hold R and stop moving right if he unhold R i dont want it to stop moving if he press another key

1 Like

it won’t, its using run service, so every frame it will check if your holding R, if you are add a value to a preset velocity and then at the end move towards that velocity direction thing, if you arent holding R then the velocity will be zero and he will stop. If you hold R and F you would go up right.

1 Like

ok but your code is not full yet you did not add a local for RunService and UserInputService

1 Like

no, it was just example code, so you add the runservice and userinputservice locals

edit: when i say it was example i mean it wasn’t designed to be used in a script without anything else, its just a snippet

1 Like

ok I will try it

(more characters to make me can send it)

1 Like

output errors `

Move is not a valid member of Model “Workspace.MONSTERGAMES3609”

`

1 Like

I meant

Character.Humanoid:Move()

oops

1 Like

Wait, quick question

Are you trying to move the Character Model, or trying to move the Character from the ground?

1 Like

I’m trying to move the character

1 Like

Try this?

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

local humanoid = Character:WaitForChild("Humanoid")
local Moving = false
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

UserInputService.InputBegan:Connect(function(keyCode, Chatted)

	if Chatted then
		return
	end

	local HRP = Character:FindFirstChild("HumanoidRootPart")

	if HRP ~= nil and keyCode.KeyCode == Enum.KeyCode.R then
		Moving = true
		
		while Moving do
			Character:MoveTo(HRP.Position + Vector3.new(0,0,1))
			RunService.RenderStepped:Wait()
		end
		
	end
end)

UserInputService.InputEnded:Connect(function(keyCode)
	local HRP = Character:FindFirstChild("HumanoidRootPart")

	if HRP ~= nil and keyCode.KeyCode == Enum.KeyCode.R then
		Moving = false
		Character:MoveTo(HRP.Position)
	end
	
end)
1 Like

That work but how to make it move slower because character move like flash lol

1 Like

@MONSTERGAMES3609 Im not sure about this but maybe you could add a wait time somewhere.

1 Like

oh nvm i just add wait(0.1) tysm

1 Like

You could potentially create a local variable, which can actually reference the Speed on how fast you want the Character to travel

Something like this could do just fine

local Speed = 0.25
Character:MoveTo(HRP.Position + Vector3.new(0, 0, Speed))
2 Likes

yea yea i add one

extra characters and ty

your welcome @MONSTERGAMES3609