-- send nil to client to mark repair cancelled


local repairTarget
local repairingValue

local apparentHolder

local requestStop = false


script.RepairChannel.OnServerEvent:Connect(function(plr, engine, valObj)
	local holder = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	
	print(holder, plr)
	
	if holder == plr then
		apparentHolder = plr
		
		if engine ~= nil then
			repairingValue = valObj
			repairTarget = repairingValue.Parent.Parent.Parent.Engine
			
			script.Internal:Fire()
		else
			requestStop = true
		end
	end
end)

while true do
	requestStop = false
	script.Internal.Event:Wait()
	
	while wait(.5) do
		local engine = repairingValue.Parent.Parent.Parent.Engine
		local dist = (script.Parent.Handle.Position - engine.Position).Magnitude
		local vel = engine.Velocity.Magnitude
		
		if requestStop or dist > 50 or vel > 3 or script.Parent.Parent:FindFirstChildOfClass("Humanoid") == nil or repairingValue.Value >= 100 then
			script.RepairChannel:FireClient(apparentHolder)
			
			repairingValue = nil
			break
		else
			repairingValue.Value = math.min(repairingValue.Value + 1, 100)
		end
	end
end
