-- ================================================================================
-- PUBLIC FUNCTIONS
-- ================================================================================

function PlayerConverter:PlayerToSpeeder(selection, racing)
	if racing == nil then racing = false end
	
	-- Fire RemoteEvent to handle conversion server-side
	LoadSpeeder:FireServer(selection, racing)
end -- PlayerConverter:PlayerToSpeeder()

function PlayerConverter:PlayerToSpeederClient(racing)
	if racing == nil then racing = false end
	 
	local Players = game:GetService("Players")
	local player = Players.LocalPlayer
	local PlayerGui = player:FindFirstChild("PlayerGui")
	
	-- Get player's character
	local character = player.Character or player.CharacterAdded():wait()
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	local head = character:FindFirstChild("Head")
	
	-- Move HumanoidRotPart out of range of interaction NPC
	if humanoidRootPart then
		humanoidRootPart.Position = humanoidRootPart.Position + Vector3.new(0, 40, 0)
	end
	if head then
		head.Position = head.Position + Vector3.new(0, 40, 0)
	end
		
	-- Lock/Center and hide mouse
	UserInputService.MouseIconEnabled = false
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false
	
	-- Disable touch control GUIs on mobile devices
	if IS_MOBILE then
		local TouchGui = PlayerGui:WaitForChild("TouchGui")
		TouchGui.Enabled = false
	end
	
	if racing then
		local countdownGui = PlayerGui:WaitForChild("CountdownGui")
		countdownGui.TimerFrame.Visible = false
	end
end -- PlayerConverter:PlayerToSpeederClient()

function PlayerConverter:PlayerToSpeederServer(player, selection, racing)
	if racing == nil then racing = false end
	
	-- Get player's character
	local character = player.Character or player.CharacterAdded:wait()
	
	-- Anchor and make limbs transparent
	setModelAnchored(character, true)
	setModelCanCollide(character, false)
	setModelTransparency(character, 1)
	
	-- Remove limbs and unecessay character parts
	removeLimbsAndThings(character)
	
	-- Clone selected speeder
	local speeder = selection:clone()
	
	-- Set character's PrimaryPart to speeder's PrimaryPart
	local body = speeder:FindFirstChild("Body")
	assert(body, "All speeders must have a part named Body. It will be used as the PrimaryPart")
	speeder.PrimaryPart = body
	character.PrimaryPart = body
	character.PrimaryPart.Name = "HumanoidRootPart"
	
	-- Set collision group so that players cannot collide when they are speeders
	setCollisionGroup(speeder, _SPEEDEROBJECTSCOLLISIONGROUP)
	
	-- Unanchor speeder
	setModelAnchored(speeder, false)
	
	-- Insert speeder model into character
	insertModelIntoCharacter(character,speeder)
	
	-- Clone speeder scripts into player
	local controller = Controller:clone()
	controller.Parent = character
	
	-- Move player to SpeederSpawnLocation
	character:SetPrimaryPartCFrame(SpeederSpawnLocation.CFrame)
	
	LoadSpeeder:FireClient(player, racing)
end -- PlayerConverter:PlayerToSpeederServer()
