Problem with car

Hello, I am creating a custom car and I would like to control it like a real car with my keyboard (W to go foward, S to go back, A to go on the left and D to go on the right).

How can I do a script with UIS that move the model on server side and to go in the direction given by the Player.

3 Likes

Use a VehicleSeat? Much better than building that from scratch

2 Likes

It does not work if I just put a vehicleSeat.

Hmm pretty sure you’ll have to set it up properly like car if you want to use vehicle seat but it’ll be less buggy

1 Like

There are properties in a VehicleSeat like ThrottleFloat and SteerFloat that store the input, so you just have to use those properties in a script that makes the car move depending on those properties

VehicleSeat documentation

1 Like

Do you have an example for guide me? I mean a tuto.

1 Like

heres a tutorial

Incase you still feel like re-inventing the wheel, this is a VERY basic example of moving models with UIS

local UserInputService = game:GetService("UserInputService")

local Car = workspace:WaitForChild("Car")

local Moving = false

UserInputService.InputBegan:Connect(function(input, _gameProceesedEvent)
	if input.KeyCode == Enum.KeyCode.W then
		if Moving then return end --> Don't Do Anything if Already Moving
		print("Started!")
		Moving = true
		while Moving == true do
			Car:PivotTo(Car:GetPivot() + Vector3.new(0,0,-1))
			print("Moving!")
			task.wait(0.01)
		end
	elseif input.KeyCode == Enum.KeyCode.S then
		if Moving then return end --> Don't Do Anything if Already Moving
		print("Started!")
		Moving = true
		while Moving == true do
			Car:PivotTo(Car:GetPivot() + Vector3.new(0,0,1))
			print("Moving!")
			task.wait(0.01)
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, _gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.W then
		if not Moving then return end
		Moving = false
	elseif input.KeyCode == Enum.KeyCode.S then
		if not Moving then return end
		Moving = false
	end
end)
1 Like

You can’t add vector3s to cframes

You can…That Code is tested and is working fine. Ofcourse you can add Vector3s in CFRames, CFrames have Position in them.

ah ok

mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

It’s Alright, Refer to these for more information about them

1 Like

Yeah but there is 4 wheels, me my car is flying.

so either you could make the wheels invisible, or you could move the car using BodyForces.

You could look up tutorials on YouTube on how to use BodyForces

1 Like
local leftWheel = script.Parent.Parent.LeftWheel
local leftWheel2 = script.Parent.Parent.LeftWheel2
local rightWheel = script.Parent.Parent.rightWeel
local rightWheel2 = script.Parent.Parent.rightWeel2

local steer = script.Parent.Parent.Steer
local speed = 80

script.Parent.Changed:Connect(function(property)
	leftWheel.AngularVelocity = speed * script.Parent.Throttle
	leftWheel2.AngularVelocity = speed * script.Parent.Throttle
	rightWheel.AngularVelocity = speed * -script.Parent.Throttle
	rightWheel2.AngularVelocity = speed * -script.Parent.Throttle
	steer.TargetAngle = 10 * script.Parent.Steer
end)

Well I am getting a little problem, it’s not stopping turning around around and when I stop typing “A” for example, it continues to turn…

https://gyazo.com/442dfb8cbc096217d32ba090fe72f437

It works with CFrame and not Vector just how I move it server side…

local UserInputService = game:GetService("UserInputService")

local Car = script.Parent.Parent

local seat = Car:FindFirstChild("MainSeat")

local rotationSpeed = 1
local Moving = false

UserInputService.InputBegan:Connect(function(input, _gameProceesedEvent)
	if input.KeyCode == Enum.KeyCode.W and not _gameProceesedEvent and seat.Occupant then
		if Moving then return end 
		Moving = true
		while Moving == true do
			Car:PivotTo(Car:GetPivot() * CFrame.new(0,0,1))
			task.wait(0.01)
		end
	elseif input.KeyCode == Enum.KeyCode.S and not _gameProceesedEvent and seat.Occupant then
		if Moving then return end 
		Moving = true
		while Moving == true do
			Car:PivotTo(Car:GetPivot() * CFrame.new(0,0,-1))
			task.wait(0.01)
		end
	elseif input.KeyCode == Enum.KeyCode.D and not _gameProceesedEvent and seat.Occupant then
		if Moving then return end
		Moving = true
		while Moving == true do
			Car:PivotTo(Car:GetPivot() * CFrame.Angles(0,-math.rad(1),0))
			task.wait(0.01)
		end
	elseif input.KeyCode == Enum.KeyCode.A and not _gameProceesedEvent and seat.Occupant then
		if Moving then return end
		Moving = true
		while Moving == true do
			Car:PivotTo(Car:GetPivot() * CFrame.Angles(0,math.rad(1),0))
			task.wait(0.01)
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, _gameProceesedEvent)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.S then
		if not Moving then return end
		Moving = false
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.