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)
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)
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
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
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.
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)