Is my soccer ball hit detection system good?

So, I recently replaced my old .Touched detection system with a new one.

local function kick()
	localPlayer:SetAttribute("Kicking", true)
	canTouch = true
	
	if localPlayer:GetAttribute("Leg") == "Right Leg" then
		rightAnimationTrack:Play(0)
	else
		leftAnimationTrack:Play(0)
	end
	
	task.delay(0.3, function()
		localPlayer:SetAttribute("Kicking", false)
		canTouch = false
	end)
end

local function onEquipped(mouse: Mouse)
	if isEquipped then
		return
	end
	
	isEquipped = true
end

local function onUnequipped()
	if not isEquipped then
		return
	end
	
	isEquipped = false
end

local function onInputBegan(input: InputObject, gameProcessedEvent: boolean)
	if gameProcessedEvent then
		return
	end
	if not isEquipped then
		return
	end
	if localPlayer:GetAttribute("Kicking") then
		return
	end
	
	local keybind = Keybinds.current[tool.Name][script.Name]
	
	if input.UserInputType == keybind or input.KeyCode == keybind then
		kick()
	end
end

local function onPreSimulation(deltaTimeSim: number)
	if canTouch then
		local leg = character[localPlayer:GetAttribute("Leg")]
		local partBoundsInBox = Workspace:GetPartBoundsInBox(leg.CFrame, leg.Size)
		
		for index, part in ipairs(partBoundsInBox) do
			if part:HasTag("Ball") then
				canTouch = false

				ToolUtilities:requestNetworkOwnership(part)

				local velocityCFrame = humanoidRootPart.CFrame

				local bodyVelocity = Instance.new("BodyVelocity")
				bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
				bodyVelocity.Velocity = (velocityCFrame.LookVector * 21.5) + (velocityCFrame.UpVector * 2)
				bodyVelocity.Parent = part

				task.delay(0.3, function()
					bodyVelocity:Destroy()
				end)
			end
		end
	end
end

tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)
RunService.PreSimulation:Connect(onPreSimulation)
UserInputService.InputBegan:Connect(onInputBegan)

Can I have some feedback? should I be initializing and disconnecting the preSimulation event inside the kick() function for performance? Is the system itself good?

1 Like