I made a soccer script for a tool that allows you to pass the ball, Is my code bad? All im doing is detecting input, detecting when the players leg touches a ball, and then setting network ownership and applying forces on client-side
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)
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)