--Player Manager

seat.ChildAdded:connect(function(it)
	if it:IsA("Weld") and it.Name == "SeatWeld" then
		SeatWeld = it
		local plr = game.Players:GetPlayerFromCharacter(it.Part1.Parent)
		if plr then
			local done = true
			if plr.PlayerGui:findFirstChild("Car") then
				done = false
			end
			if done then
				Player = plr
				local gui = car:Clone()
				gui.Frame.Seat.Value = seat
				gui.Parent = plr.PlayerGui
				gui.Frame.LocalScript.Disabled = false
				wait(1)
				if not it.Parent then
					endit()
				end
			else
				if it.Part1.Parent:FindFirstChild("Humanoid") then
					it.Part1.Parent.Humanoid.Jump = true
				end
			end
		end
	end
end)
seat.ChildRemoved:connect(function(it)
	if it == SeatWeld then
		endit(true)
	end
end)

function endit(plr)
	SeatWeld = nil
	Throttle = 0
	Reverser = 0
	updatewheels()
	if plr then
		if Player and Player.PlayerGui:FindFirstChild("Car") then
			Player.PlayerGui.Car:Destroy()
		end
		Player = nil
		if Bell and not BellPersistsAfterLeaving then
			script.Parent.Locotrol:Fire({["Bell"]=false})
		end
		if Horn then
			script.Parent.Locotrol:Fire({["Horn"]=false})
		end
	end
end

function FireLocotrol()
	script.Parent.Locotrol:Fire({["Rev"]=Reverser,["Th"]=Throttle,["Origin"]=seat}) --,["Br"]=Brakef,["Origin"]=seat})
end

function control(plr,tbl)
	if plr ~= Player then return end --print("plr is not Player") return end
		if tbl["Rev"] then
			if Player then
				Reverser = tbl["Rev"]
			else
				--print("no player in house")
				--print("Origin is "..tbl["Origin"]:GetFullName())
				for _,i in pairs ({FrontCoupler,RearCoupler}) do
					if tbl["Origin"] == i.drive then
						--print("found our seat")
						Reverser = tbl["Rev"] * (i.Reversed and -1 or 1)
					end
				end
			end
		end
		if tbl["Th"] then
			Throttle = tbl["Th"]
		end
		if tbl["Horn"]~=nil then
			Horn = tbl["Horn"]
			script.Parent.Locotrol:Fire({["Horn"]=tbl["Horn"]})
		end
		if tbl["Bell"]~=nil then
			Bell = tbl["Bell"]
			script.Parent.Locotrol:Fire({["Bell"]=tbl["Bell"]})
		end
		if tbl["Engine"]~=nil then
			Engine = tbl["Engine"]
			script.Parent.Locotrol:Fire({["Engine"]=tbl["Engine"]})
		end
		if tbl["Lights"]~=nil then
			Lights = tbl["Lights"]
			script.Parent.Locotrol:Fire({["Lights"]=tbl["Lights"]})
		end
		if tbl["Lights2"]~=nil then --Added additional lights
			Lights = tbl["Lights2"]
			script.Parent.Locotrol:Fire({["Lights2"]=tbl["Lights2"]})
		end
		if tbl["Lights3"]~=nil then
			Lights = tbl["Lights3"]
			script.Parent.Locotrol:Fire({["Lights3"]=tbl["Lights3"]})
		end
		if tbl["Lights4"]~=nil then
			Lights = tbl["Lights4"]
			script.Parent.Locotrol:Fire({["Lights4"]=tbl["Lights4"]})
		end
	updatewheels()
	FireLocotrol()
end

script.Parent.Controller.OnServerEvent:connect(function(plr,tbl)
	control(plr,tbl)
end)

wait(1)
getwheels(script.Parent.Parent)
updatewheels()
seat.Changed:connect(function()	
	MAXIMUMSPEED=seat.MaxSpeed
	TORQUE=seat.Torque
	updatewheels()
end)
workspace.DescendantAdded:connect(function(it)
	for _,i in pairs ({FrontCoupler,RearCoupler}) do
		spawn(function()
			wait(0.5)
			if not it:IsA("ManualWeld") then return end
			if it.Part1.Parent == i.model or it.Part0.Parent == i.model then
				i.weld = it
				if not i.coupledto and it.Parent then
					local them = ((it.Part0.Parent == i.model) and it.Part1.Parent or it.Part0.Parent)
					i.coupledto = them
					local themSeat = scopinggetDrive(them)
					if themSeat == seat then
						--print("oops found myself")
						endit()
						return
					end
					i.drive = themSeat
					--print(i.drive:GetFullName())
					if not themSeat:FindFirstChild("SeatWeld") then
						wait(0.1)
						FireLocotrol()
					end
					local couplers = scopinggetcoupler(themSeat.Parent)
					local f,r = getclosestcoupler(couplers[1],couplers[2])
					i.Reversed = (i.front == (f == i.coupledto))
					if themSeat:FindFirstChild("Locotrol") then
						i.GetControls = themSeat.Locotrol.Event:connect(function(tbl)
							local d = tick()
							if d-i.LastUpdate > 0.06 then
								i.LastUpdate = d
								if not seat:FindFirstChild("SeatWeld") then
									control(nil,tbl)
								end
							end
						end)
					end
					i.WeldRemoved = it.AncestryChanged:connect(function(_,pr)
						if pr ~= workspace then
							i.WeldRemoved:disconnect()
							if i.GetControls then
								i.GetControls:disconnect()
							end
							i.GetControls = nil
							i.drive = nil
							i.coupledto = nil
							--print("WeldRemoved!")
							endit(true)
						end
					end)
					--print("Coupler all set up!")
				end
			end
		end)
	end
end)
wait(5) --changed from 60 to 5 to avoid issues with coupler welds moving to workspace and give time for other scripts to update before moving to workspace.
script.Parent.Parent.Parent = workspace --this moves it to workspace, if coupled before wait time, it removes coupler welds.
