Move a custom car (+ Server Side)

Hello, I would like to know how can I use my localscript in my model (a script that move the model from keys input with UIS). I mean, I would like to have the script enable for the player that is on the main Seat and can move the car only if the player is on it :


image

check if there is a player on it by using the Occupant property of the Seat object

You can’t move a model with a script by changing pivots so the only way you can do it is by having a origin part in the middle of the car and have every part welded to it and then you just move that part and everything else will move with it.

We can move a model with PivotTo :

Localscript (without remotes):

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(rotationSpeed),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(rotationSpeed),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)
1 Like

Why are you moving a model in a client script?

Using remotes for each models is not a good thing… So someone is talking about SetNetworkOwner() and a script that has RunContextClient.

I mean, checking the user’s input in a client script, to then transfer to the server and move the model, is the best and probably only option.

image
After using car : (server side)

You have 2 scripts (from Server Script ) :

  • One I gave before that get inputs from user like a Localscript.
  • The last is a way to don’t use remote events to ask for every frames :
game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.Humanoid.Seated:Connect(function(active, seat)
			if seat == nil then return end 
			if seat.Parent == script.Parent.Parent then 
				seat:SetNetworkOwner(plr)
				print(active, plr.Name)
			end
		end)
	end)
end)

https://gyazo.com/fe46aad6a5e990e1413f65c615ccfdee

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