Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make it so if you press a key a part will continue to move?

Im new to scripting so im very bad at it but I have a problem where I want to make a car (I know theres a classic way to make it without scripting but I dont want to make one like that) so if I press the WASD buttons it will move, and the first thing i want to do it to make it move in any direction when I press W. The code for detecting a key works (I added a KeyRemote called KeyDown in replicated storage) and the local script is in StarterGui, but I put a script in ServerScriptService and the code that made the part move didnt work.

Car moving script (dosen't work)

local KeyDownEvent = game:GetService("ReplicatedStorage"):WaitForChild("KeyDown") -- Defines the RemoteEvent
local car = game.Workspace.car

KeyDownEvent.OnServerEvent:Connect(function() -- This function runs when a player fires it
	
	car.Position = car.Vector3.new(-1,-1,-1)
	
end)

Key Detection script (Works)

local UIS = game:GetService("UserInputService") -- Gets the UserInputService that detects the players inputs

local KeyDownEvent = game:GetService("ReplicatedStorage"):WaitForChild("KeyDown") -- Defines the RemoteEvent


UIS.InputBegan:Connect(function(input)   -- This function runs when the player presses a key
	if input.KeyCode == Enum.KeyCode.W then   -- Checks if the pressed key was E

		KeyDownEvent:FireServer()   -- Tells the server that the player pressed E

	end
end)

3 answers

Well first of all your using car.Position. When moving a part or a model with a script you need to make use of CFrame. The second preoblem is your using car.Vector3.new() when it should really be just Vector3.new(). However as stated last, you should be using CFrame so make use of CFrame.new.

Alongside this as your pressing W you would want it to move forward so you would make use of the cars LookVector instead of setting the position.

If the car is a model, make sure everything is wielded together and then set a primary part. Make sure said primary part is facing forward, which there is a few plugins for this, then change your script to look like this:

local KeyDownEvent = game:GetService("ReplicatedStorage"):WaitForChild("KeyDown")
local car = game.Workspace.car
 
KeyDownEvent.OnServerEvent:Connect(function() -- This function runs when a player fires it
 
    car.PrimaryPart.CFrame = CFrame.new(car.PrimaryPart.CFrame.LookVector * 1)
 
end)


Else if its a mesh just make sure the front of the MeshPart or Part with a mesh in is the front of the car then use:

local KeyDownEvent = game:GetService("ReplicatedStorage"):WaitForChild("KeyDown")
local car = game.Workspace.car
 
KeyDownEvent.OnServerEvent:Connect(function() -- This function runs when a player fires it
 
    car.CFrame = CFrame.new(car.CFrame.LookVector * 1)
 
end)

1 comment

0

The code worked for changing the position but my question was how do I make it to continue moving? Like how a normal car keeps moving when you make it go im wondering how to make a script like that, and when you let go of the key it stops, from when i play it i see its putting it somewhere else. I think im asking too much but Im sry idrk scripting at all.

Hello, Use variable values in your scripts to check if the key is down then use a while loop to check if it's still down then fire it!

LocalScript:

local replicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local keyDownRE = replicatedStorage:FindFirstChild("KeyDown")

local isMovingForward = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.W then
		if not gameProcessed then -- Check if the player isn't using the chat or anything else!
			
			if isMovingForward == false then
				
				isMovingForward = true
				
			end
			
		else
			print("Player is typing")
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		
		if isMovingForward == true then
			
			isMovingForward = false
			
		end
		
	end
end)

while isMovingForward do
	
	keyDownRE:FireServer()
	
	wait(1)
	
end

Script:

local replicatedStorage = game:GetService("ReplicatedStorage")

local car = workspace:FindFirstChild("Car")

replicatedStorage:FindFirstChild("KeyDown").OnServerEvent:Connect(function(player)
	
	car.CFrame = CFrame.new(car.CFrame.LookVector * 1)
	
end)

3 comments

0

Sry but the code didn't work when I tried it. Idk whats wrong with it but when I pressed W it did nothing

0

Edited, should work now

0

Nothing

hey you! have you ever heard of enes? if you are in trouble, better call enes!

1 comment

0

well im in trouble

This discussion is preserved as it was. New answers, comments, and votes are closed.