Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago
Edited 2 years ago

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)

1local KeyDownEvent = game:GetService("ReplicatedStorage"):WaitForChild("KeyDown") -- Defines the RemoteEvent
2local car = game.Workspace.car
3 
4KeyDownEvent.OnServerEvent:Connect(function() -- This function runs when a player fires it
5 
6    car.Position = car.Vector3.new(-1,-1,-1)
7 
8end)

Key Detection script (Works)

01local UIS = game:GetService("UserInputService") -- Gets the UserInputService that detects the players inputs
02 
03local KeyDownEvent = game:GetService("ReplicatedStorage"):WaitForChild("KeyDown") -- Defines the RemoteEvent
04 
05 
06UIS.InputBegan:Connect(function(input)   -- This function runs when the player presses a key
07    if input.KeyCode == Enum.KeyCode.W then   -- Checks if the pressed key was E
08 
09        KeyDownEvent:FireServer()   -- Tells the server that the player pressed E
10 
11    end
12end)

3 answers

Log in to vote
0
Answered by 2 years ago

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:

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

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:

1local KeyDownEvent = game:GetService("ReplicatedStorage"):WaitForChild("KeyDown")
2local car = game.Workspace.car
3 
4KeyDownEvent.OnServerEvent:Connect(function() -- This function runs when a player fires it
5 
6    car.CFrame = CFrame.new(car.CFrame.LookVector * 1)
7 
8end)
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. ilovekidcity 10 — 2y
Ad
Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
2 years ago
Edited 2 years ago

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:

01local replicatedStorage = game:GetService("ReplicatedStorage")
02local UserInputService = game:GetService("UserInputService")
03 
04local keyDownRE = replicatedStorage:FindFirstChild("KeyDown")
05 
06local isMovingForward = false
07 
08UserInputService.InputBegan:Connect(function(input, gameProcessed)
09    if input.KeyCode == Enum.KeyCode.W then
10        if not gameProcessed then -- Check if the player isn't using the chat or anything else!
11 
12            if isMovingForward == false then
13 
14                isMovingForward = true
15 
View all 42 lines...

Script:

1local replicatedStorage = game:GetService("ReplicatedStorage")
2 
3local car = workspace:FindFirstChild("Car")
4 
5replicatedStorage:FindFirstChild("KeyDown").OnServerEvent:Connect(function(player)
6 
7    car.CFrame = CFrame.new(car.CFrame.LookVector * 1)
8 
9end)
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 ilovekidcity 10 — 2y
0
Edited, should work now MattVSNNL 620 — 2y
0
Nothing ilovekidcity 10 — 2y
Log in to vote
0
Answered by
enes223 327 Moderation Voter
2 years ago

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

0
well im in trouble ilovekidcity 10 — 2y

Answer this question