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)
1 | local KeyDownEvent = game:GetService( "ReplicatedStorage" ):WaitForChild( "KeyDown" ) -- Defines the RemoteEvent |
2 | local car = game.Workspace.car |
3 |
4 | KeyDownEvent.OnServerEvent:Connect( function () -- This function runs when a player fires it |
5 |
6 | car.Position = car.Vector 3. new(- 1 ,- 1 ,- 1 ) |
7 |
8 | end ) |
Key Detection script (Works)
01 | local UIS = game:GetService( "UserInputService" ) -- Gets the UserInputService that detects the players inputs |
02 |
03 | local KeyDownEvent = game:GetService( "ReplicatedStorage" ):WaitForChild( "KeyDown" ) -- Defines the RemoteEvent |
04 |
05 |
06 | UIS.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 |
12 | end ) |
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:
1 | local KeyDownEvent = game:GetService( "ReplicatedStorage" ):WaitForChild( "KeyDown" ) |
2 | local car = game.Workspace.car |
3 |
4 | KeyDownEvent.OnServerEvent:Connect( function () -- This function runs when a player fires it |
5 |
6 | car.PrimaryPart.CFrame = CFrame.new(car.PrimaryPart.CFrame.LookVector * 1 ) |
7 |
8 | 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:
1 | local KeyDownEvent = game:GetService( "ReplicatedStorage" ):WaitForChild( "KeyDown" ) |
2 | local car = game.Workspace.car |
3 |
4 | KeyDownEvent.OnServerEvent:Connect( function () -- This function runs when a player fires it |
5 |
6 | car.CFrame = CFrame.new(car.CFrame.LookVector * 1 ) |
7 |
8 | end ) |
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:
01 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local UserInputService = game:GetService( "UserInputService" ) |
03 |
04 | local keyDownRE = replicatedStorage:FindFirstChild( "KeyDown" ) |
05 |
06 | local isMovingForward = false |
07 |
08 | UserInputService.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 |
Script:
1 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
2 |
3 | local car = workspace:FindFirstChild( "Car" ) |
4 |
5 | replicatedStorage:FindFirstChild( "KeyDown" ).OnServerEvent:Connect( function (player) |
6 |
7 | car.CFrame = CFrame.new(car.CFrame.LookVector * 1 ) |
8 |
9 | end ) |
hey you! have you ever heard of enes? if you are in trouble, better call enes!