-- Basic settings

local ROTATION_SPEED = math.pi * 4.5

local plr = game.Players.LocalPlayer
local tweenService = game:GetService("TweenService")

game.ReplicatedStorage.Interactions.Client.BeginKnifeThrow.OnClientEvent:connect(function(knife, rotateAxis, velocity)
	
	-- Connect end knife throw
	local stopLoop = false
	local finalCFrame = nil
	local endEvent = game.ReplicatedStorage.Interactions.Client.EndKnifeThrow.OnClientEvent:connect(function(otherKnife, cframe)
		if otherKnife == knife then
			stopLoop = true
			finalCFrame = cframe
		end
	end)
	
	-- Make knife move smoothly
	local currentTween = nil
	while not stopLoop do
	
		-- Wait
		local waitTime = wait()
		
		-- Tween
		local rotateAmt = rotateAxis * (-ROTATION_SPEED) * waitTime
		local goal = {
			CFrame = (knife.CFrame + (velocity * waitTime)) * CFrame.fromEulerAnglesXYZ(rotateAmt.X, rotateAmt.Y, rotateAmt.Z)
		}
		currentTween = tweenService:Create(knife, TweenInfo.new(waitTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), goal)
		currentTween:Play()
		
	end
	
	-- Disconnect event
	endEvent:disconnect()
	
	-- End tween
	if currentTween ~= nil then
		currentTween:Cancel()
	end
	
	-- Set final CFrame
	if knife.Parent ~= nil and finalCFrame ~= nil then
		knife.CFrame = finalCFrame
	end
	
end)
