Multiple vehicles and tanks

I’m having difficulty understanding how to put turrets on vehicles and then allow multiple players to spawn the same vehicle, because for the shooting part of the turret and rotation and all, I’ll need to use remote events, but If I use remote events, then how will I be able to allow multiple players to spawn the same kind of vehicle? because the events would be specific to one vehicle, thus the other vehicles wouldn’t work, but I see the whole time in other games that player1 can spawn for example lets say a tank, and then player2 also spawns that same tank, like in armored patrol and in base battles.

So how is this done?

Any help appreciated!

(let me know if I should be more clear)

1 Like

You can get the path to the specific unique instance for each unique vehicle model Instance.

One possible path goes from player character → Humanoid.SeatPart → find the vehicle model with .Parent or :FindFirstAncestor → find first child to a turret part or attachment.

Another method is to use a Seat:GetPropertyChangedSignal(“Occupant”).

This should be enough to keep track of which model the character is driving whether in the server or client.

Like this?


local LocalPlayer = game.Players.LocalPlayer
local character = LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

while wait() do
	if humanoid.SeatPart.Parent.name == "tank" then
		print("player is sitting in tank")
	end
end

I get this error: attempt to index nil with 'Parent'

This method is for the server right?

Also, how do I get the player reference from this method? Like to specify the player, so I can send it through a remote.

For the tank I made it will clone a script to the occupant and then remove it when they jump out.

How did you specify the occupant?

Seat.Occupant is the seat’s occupant. To manually set an occupant, you may use Seat:Sit().

So if I wanted to detect if the seat.Occupant has clicked and then fire a bullet, I would have to send the seat.occupant through a remote event to a local script, then detect the click, then fire another remote event to the server to verify the click and cool down etc, then fire another remote event to a client script which will handle the projectile.

Yes?

You don’t send seat.Occupant to a local script. You send the local script to seat.Occupant!

You don’t need to, and it is not recommended to do so. You can just use the server to verify the click.

Could you explain what you mean?

You clone a LocalScript to the occupant so it can run. It will not run in the tank.

And that local script should contain the listener to check if the player clicks?

Also, if the player does click, I will have to fire a remote event to the server to be able to fire a “fire all clients” to create the projectile.

And I wont lose the player that’s sitting while its going through so many remotes?

The local script doesn’t listen. Alright, flip your whole world upside down and you will come closer to understanding.

The local script sends the event from clicking, the server does not.
The server listens to the clicking event from the local script.

This is why I didn’t use that for my tank. All the server listens to is the click and it does not listen for mouse movement, and the local script uses AlignOrientation to orient the turret correctly, as it is replicated.

Could I have a look at this local script?

Here:

local mouse = game.Players.LocalPlayer:GetMouse()
wait(.5)
local tank = script.WhichTank.Value
local old = workspace.CurrentCamera.CameraType
local char = script.Parent
local origTorque = tank.MainSeat.Torque
local help = script.HelpGui
function End()
	mouse.Icon=""
	if help then
		help:Destroy()
	end
	if tank.MainSeat then
		tank.MainSeat.Torque=origTorque
	end
	workspace.CurrentCamera.CameraType=old
	script:Destroy()
end





local turret = tank:FindFirstChild("Turret")
if not turret then
	End()
end
local spawnPos = turret:FindFirstChild("MissileSpawnPos")
if not spawnPos then
	End()
end
local fire = tank.Turret.fire
local change = tank.Turret.updateTurret
local gyroX = tank.Turret.GyroX.GyroX
local gyroY = tank.Turret.GyroY.GyroY
local mode = turret.Mode
reloadIcon="rbxassetid://2502466"

mainIcon="rbxassetid://2302147960"

mouse.Icon=mainIcon


local reloading = false

help.Parent=game.Players.LocalPlayer.PlayerGui

local toggle = false
local toggle2 = false
local down = false

mouse.Button1Down:Connect(function()
	if reloading==false and turret and spawnPos then
		down = true
		pcall(function()
			while down do
				local lookAt = CFrame.lookAt(turret.Gun.Position, spawnPos.Position)
				fire:InvokeServer(lookAt)
				mouse.Icon=reloadIcon
				reloading=true
				wait(turret.ReloadTime.Value)
				reloading=false
				mouse.Icon=mainIcon
				if mode.Value~="Auto"then
					break
				end
			end
		end)
	end
end)
mouse.Button1Up:Connect(function()
	down = false
end)
mouse.KeyDown:Connect(function(key)
	key=string.lower(key)
	if key=="n" then
		if toggle2==false then
			tank.MainSeat.engine.Playing=toggle2
			toggle2=true
			tank.MainSeat.Torque=0
		else
			tank.MainSeat.engine.Playing=toggle2
			toggle2=false
			tank.MainSeat.Torque=origTorque
		end
	end
end)
xpcall(function()
	while char.Humanoid.SeatPart==tank.MainSeat do
		game:GetService("RunService").RenderStepped:Wait()
	
		local gyroXpos = CFrame.lookAt(turret.middle.Position, mouse.Hit.Position, turret.GyroX.CFrame.UpVector)
		local gyroYpos = CFrame.lookAt(turret.GyroY.Position, mouse.Hit.Position, turret.GyroY.CFrame.UpVector)
	
		--change:FireServer(gyroXpos, gyroX)
		--change:FireServer(gyroYpos, gyroY)
		turret.GyroX.GyroX.CFrame = gyroXpos
		turret.GyroY.GyroY.CFrame = gyroYpos
	
		if not spawnPos or not turret then
			mouse.Icon=""
		end
	end
	End()
end, End)