Optimize my soccer script/any changes?

I made a soccer script for a “pass” tool that allows you to pass the ball. Are there any changes I should make or things that can be improved?

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerService = game:GetService("Players")
local Debris = game:GetService("Debris")

local MAX_POWER = 80

local localPlayer = PlayerService.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local tool = script.Parent

local isInputActivated = true
local expectingInput = false
local isDoingAction = false
local power


local function onEquipped(mouse)
	expectingInput = true
end


local function onUnequipped()
	expectingInput = false
end


local function onInputBegan(input, isProcessed)
	if isProcessed or not expectingInput then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		isInputActivated = true
		power = 25
		while isInputActivated do
			power = math.min(power + 1, MAX_POWER)
			print(power)
			task.wait(1/60)
		end
	end
end


local function onInputEnded(input, isProcessed)
	if isProcessed or not expectingInput then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		isInputActivated = false
		isDoingAction = true
		character.Torso.CollisionGroup = "Character"
		task.delay(.75, function()
			isDoingAction = false
			character.Torso.CollisionGroup = "Default"
		end)
	end
end


local function onTouched(hit)
	if hit.Name ~= "Ball" or not isDoingAction then return end
	isDoingAction = false
	ReplicatedStorage.Events.UpdateOwner:FireServer(hit)
	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bodyVelocity.Velocity = humanoidRootPart.CFrame.LookVector*power + humanoidRootPart.CFrame.UpVector*(power * .035)
	bodyVelocity.Parent = hit
	Debris:AddItem(bodyVelocity, .35)
	local ballPosition = hit.Position + Vector3.new(0, humanoidRootPart.Position.Y - hit.Position.Y, 0)
	local dotProduct = humanoidRootPart.CFrame.LookVector:Dot(ballPosition - humanoidRootPart.Position)
	local torsoAngle = math.acos(dotProduct / (ballPosition - humanoidRootPart.Position).Magnitude)
	local rightDistance = (hit.Position - character["Right Arm"].Position).Magnitude
	local leftDistance = (hit.Position - character["Left Arm"].Position).Magnitude
	if torsoAngle > math.rad(40) and leftDistance < rightDistance then
		local bodyForce = Instance.new("BodyForce")
		bodyForce.Force = humanoidRootPart.CFrame.RightVector * -80
		bodyForce.Parent = hit
		local bodyAngularVelocity = Instance.new("BodyAngularVelocity")
		bodyAngularVelocity.MaxTorque = Vector3.new(0, math.huge, 0)
		bodyAngularVelocity.AngularVelocity = Vector3.new(0, (power / MAX_POWER) * 70, 0)
		bodyAngularVelocity.Parent = hit
		Debris:AddItem(bodyForce, power/MAX_POWER)
		Debris:AddItem(bodyAngularVelocity, power/MAX_POWER)
	elseif torsoAngle > math.rad(50) and rightDistance < leftDistance then
		local bodyForce = Instance.new("BodyForce")
		bodyForce.Force = humanoidRootPart.CFrame.RightVector * 80
		bodyForce.Parent = hit
		local bodyAngularVelocity = Instance.new("BodyAngularVelocity")
		bodyAngularVelocity.MaxTorque = Vector3.new(0, math.huge, 0)
		bodyAngularVelocity.AngularVelocity = Vector3.new(0, (power / MAX_POWER) * -70, 0)
		bodyAngularVelocity.Parent = hit
		Debris:AddItem(bodyForce, power/MAX_POWER)
		Debris:AddItem(bodyAngularVelocity, power/MAX_POWER)
	end
end

tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)
character["Right Leg"].Touched:Connect(onTouched)
character["Left Leg"].Touched:Connect(onTouched)
UserInputService.InputBegan:Connect(onInputBegan)
UserInputService.InputEnded:Connect(onInputEnded)
4 Likes

Since there’s only 2 lines of code within the if statement that are different, it doesn’t really make sense to write out that twice.


And since it’s just a swap of negative and positive, you can do something along the lines of:

local directionMultiplier = 0

if torsoAngle > math.rad(40) and leftDistance < rightDistance then
	directionMultiplier = -1
elseif torsoAngle > math.rad(50) and rightDistance < leftDistance then
	directionMultiplier = 1
end

if directionMultiplier ~= 0 then
	local bodyForce = Instance.new("BodyForce")
	bodyForce.Force = humanoidRootPart.CFrame.RightVector * 80 * directionMultiplier
	bodyForce.Parent = hit
	local bodyAngularVelocity = Instance.new("BodyAngularVelocity")
	bodyAngularVelocity.MaxTorque = Vector3.new(0, math.huge, 0)
	bodyAngularVelocity.AngularVelocity = Vector3.new(0, (power / MAX_POWER) * -70 * directionMultiplier, 0)
	bodyAngularVelocity.Parent = hit
	Debris:AddItem(bodyForce, power/MAX_POWER)
	Debris:AddItem(bodyAngularVelocity, power/MAX_POWER)
end

You can also one line the directionMultiplier variable with Binary Operations, up to you though

local directionMultiplier = torsoAngle > math.rad(40) and leftDistance < rightDistance and -1
	or torsoAngle > math.rad(50) and rightDistance < leftDistance and 1
	or 0

if directionMultiplier ~= 0 then
	local bodyForce = Instance.new("BodyForce")
	bodyForce.Force = humanoidRootPart.CFrame.RightVector * 80 * directionMultiplier
	bodyForce.Parent = hit
	local bodyAngularVelocity = Instance.new("BodyAngularVelocity")
	bodyAngularVelocity.MaxTorque = Vector3.new(0, math.huge, 0)
	bodyAngularVelocity.AngularVelocity = Vector3.new(0, (power / MAX_POWER) * -70 * directionMultiplier, 0)
	bodyAngularVelocity.Parent = hit
	Debris:AddItem(bodyForce, power/MAX_POWER)
	Debris:AddItem(bodyAngularVelocity, power/MAX_POWER)
end
2 Likes