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)