Percentage Addon

Hello Devs, So I’m making a basketball shooting system. And I’m want to upgrade the percentage system to where the percentage is also affected by how far the player’s distance from the goal is. I have no idea how I should add/approach this. Please help.

Code:

-- Services --
local Tween = game:GetService('TweenService')
local Replicated = game:GetService('ReplicatedStorage')
local Storage = game:GetService('ServerStorage')
local Folder = Replicated:FindFirstChild('Remotes')
local Modules = Replicated:FindFirstChild('Modules')
---

-- Shoot Event --
Folder.Shoot.OnServerEvent:Connect(function(Player,Bool,State,CountDown)
	--
	local Character = Player.Character
	local ShootVal = Character:FindFirstChild('Values').Shooting
	--
	local Glide = Character:WaitForChild('SmoothMovement')
	local Behavior = Character:FindFirstChild('Values').Arch
	--
	local ShotBarUI = Character.HumanoidRootPart:FindFirstChild('ShotBar')
	local BackGroundUI = ShotBarUI.Main
	local BarUI = BackGroundUI.Bar
	local PercentUI = ShotBarUI.Percent
	--
	local TargetModule = require(Modules:FindFirstChild('TargetModule'))
	local Target = TargetModule:Goal(Character:FindFirstChild('Values').TeamGoal.Value,Character.HumanoidRootPart,Player.Team.Name)
	--
	local Anims = Character.Servers:FindFirstChild('AnimCore'):FindFirstChild('GetAnim'):Invoke()
	local Connect = {}
	--
	local StartInfo = TweenInfo.new(Anims['Jumpshot'].Length / 2,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,true,0)
	local StopInfo = TweenInfo.new(0,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
	local StartTween = Tween:Create(BarUI,StartInfo,{Size = UDim2.new(1,0,1,0), Position = UDim2.new(0,0,0,0)})
	local StopTween = Tween:Create(BarUI,StopInfo,{Size = UDim2.new(BarUI.Size.X.Scale,0,BarUI.Size.Y.Scale,0), Position = UDim2.new(0,0,BarUI.Position.Y.Scale,0)})
	-- Functions --
	local function OffsetMath(BarSize,Percent)
		local Maximum = 0
		local Minimum = 2.25
		local Miss = 0
		--
		if BarSize >= 0 and BarSize <= 1 then
			Miss = (Percent*(Maximum - -Minimum)/100) + -Minimum
		end
		return Miss
	end
	--
	local function Release(Ball,RootPart,Target,BarSize,Percent)
		--
		local Magnitude = (Ball.Position - Target.Position).Magnitude
		local T = 1.1 + (Magnitude / 110)
		local G = Vector3.new(0,-game.Workspace.Gravity / math.pi,0)
		local V0 = (Target.Position - Ball.Position - 0.5 *G*T*T) / T / 1.045
		local Offset = OffsetMath(BarSize,Percent)
		--
		Ball.Connect.Part0 = nil
		Ball.Parent = game.Workspace
		Ball.Velocity = V0 + Vector3.new(Offset,Offset,Offset)
		Ball.RotVelocity = Vector3.new(0,0,25)
		PercentUI.Visible = true
		wait(CountDown)
		Ball.Values.PartParent.Value = ''
	end
	--
	local function PercentMath(BarSize)
		if BarSize >= 0 and BarSize <= 1 then
			BarSize = 0.5 - (0.5 - BarSize) * 1
		end
		--
		local Base = BarSize * 100
		local Percent = math.clamp((Base - Behavior.Value) - Character:FindFirstChild('Values').Contest.Value,0,100)
		local RoundedPercent = math.floor(Percent)
		--
		PercentUI.Text = RoundedPercent .. '%'
		return Percent
	end
	--
	local Gyro = Instance.new('BodyGyro',Character.HumanoidRootPart)
	Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
	Gyro.P = 7500
	Gyro.D = 500
	--
	if State == 1 then
		ShootVal.Value = true
		Gyro.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Vector3.new(Target.Position.X,Character:FindFirstChild('HumanoidRootPart').Position.Y,Target.Position.Z))
		--
		local Mag = (Character:FindFirstChild('HumanoidRootPart').Position - Target.Position).Magnitude
		if Mag <= 20 then
			Behavior.Value = -12
			Character.Humanoid.WalkSpeed = 8
			Anims['FarFloat']:Play(0.25)
			Connect['FarFloat'] = Anims['FarFloat'].KeyframeReached:Connect(function(Frame)
				if Frame == 'Release' then
					Anims['FarFloat']:AdjustSpeed(1)
					local Percent = PercentMath(BarUI.Size.Y.Scale)
					Release(Character:FindFirstChild('Ball'),Character.HumanoidRootPart,Target,BarUI.Size.Y.Scale,Percent)
					--
					Gyro:Destroy()
					Connect['FarFloat']:Disconnect()
					ShootVal.Value = false
				end
			end)
		else
			Character.Humanoid.WalkSpeed = 8
			if Character:FindFirstChild('Humanoid').MoveDirection == Vector3.new(0,0,0) then
				Behavior.Value = 5
			else
				Behavior.Value = 35
			end
			Anims['Jumpshot']:Play(0.25)
			Connect['Jumpshot'] = Anims['Jumpshot'].KeyframeReached:Connect(function(Frame)
				if Frame == 'Hold' then
					wait()
				end
				if Frame == 'Release' then
					Character.Humanoid.WalkSpeed = 0
					Anims['Jumpshot']:AdjustSpeed(1)
					local Percent = PercentMath(BarUI.Size.Y.Scale)
					Release(Character:FindFirstChild('Ball'),Character:FindFirstChild('HumanoidRootPart'),Target,BarUI.Size.Y.Scale,Percent)
					--
					Gyro:Destroy()
					Connect['Jumpshot']:Disconnect()
					ShootVal.Value = false
				end
			end)
		end
		--
		ShotBarUI.Enabled = true
		Glide.Disabled = false
		StartTween:Play()
		--
		wait(Anims['Jumpshot'].Length + 1.25)
		--
		ShootVal.Value = false
		BarUI.Size = UDim2.new(1,0,0,0)
		BarUI.Position = UDim2.new(0,0,1,0)
		--
		ShotBarUI.Enabled = false
		Glide.Disabled = true
		Character.Humanoid.WalkSpeed = 16
		--
	elseif State == 2 then
		Anims['Jumpshot']:AdjustSpeed(1)
		StopTween:Play()
	end
end)
---
2 Likes