RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a car with max speed 20, add a function to move the car with w,s,a,d"
local car = Instance.new("Model") car.Name = "Car" car.Parent = game.Workspace local body = Instance.new("Part") body.Name = "Body" body.Parent = car body.Size = Vector3.new(4, 2, 8) body.Position = Vector3.new(0, 1, 0) local wheel1 = Instance.new("Part") wheel1.Name = "Wheel1" wheel1.Parent = car wheel1.Size = Vector3.new(2, 2, 2) wheel1.Position = Vector3.new(2, 0.5, 3) local wheel2 = Instance.new("Part") wheel2.Name = "Wheel2" wheel2.Parent = car wheel2.Size = Vector3.new(2, 2, 2) wheel2.Position = Vector3.new(-2, 0.5, 3) local wheel3 = Instance.new("Part") wheel3.Name = "Wheel3" wheel3.Parent = car wheel3.Size = Vector3.new(2, 2, 2) wheel3.Position = Vector3.new(2, 0.5, -3) local wheel4 = Instance.new("Part") wheel4.Name = "Wheel4" wheel4.Parent = car wheel4.Size = Vector3.new(2, 2, 2) wheel4.Position = Vector3.new(-2, 0.5, -3) local maxSpeed = 20 local throttle = 0 local steering = 0 local function moveCar() local speed = throttle * maxSpeed body.CFrame = body.CFrame + body.CFrame.lookVector * speed body.CFrame = body.CFrame * CFrame.Angles(0, math.rad(steering), 0) end game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessed) if gameProcessed then return end if input.KeyCode == Enum.KeyCode.W then throttle = 1 elseif input.KeyCode == Enum.KeyCode.S then throttle = -1 elseif input.KeyCode == Enum.KeyCode.A then steering = -1 elseif input.KeyCode == Enum.KeyCode.D then steering = 1 end end) game:GetService("UserInputService").InputEnded:Connect(function(input, gameProcessed) if gameProcessed then return end if input.KeyCode == Enum.KeyCode.W then throttle = 0 elseif input.KeyCode == Enum.KeyCode.S then throttle = 0 elseif input.KeyCode == Enum.KeyCode.A then steering = 0 elseif input.KeyCode == Enum.KeyCode.D then steering = 0 end end) game:GetService("RunService").Heartbeat:Connect(function() moveCar() end)