Is my soccer kick script bad?

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)
4 Likes

i dont recommend using touched event for the hit detection, it can break at high speeds and is also unreliable in my opinion, you’d end up better with reworking ur logic to use something like GetPartsInPart or GetPartBoundsInRadius

EDIT: What @wastbo said is completely right, you should just hide the ball on the server and do all of the rendering on the client side

3 Likes

it would probably look laggy while you’re using it, I would either keep the owner as the server or do everything client sided with some networking. Also instead of detecting when each leg touches something you can just detect when the ball touches something

2 Likes

Doing everything client side seems a little complicated though and Im not at that level, could you explain more in depth

1 Like

It’d be hard to explain over text, but you can still get it somewhat smooth without

2 Likes

How can I make it smooth without doing that?

1 Like

If you set the network owner to the server (nil) it’ll be a little delayed when they kick it, but it won’t stutter when the ball gets close to them

1 Like

Yeah but the whole point of setting network owner to the player kicking it is to keep it smooth, the server handles physics very slowly/laggy

1 Like

It’d be laggier to have it switch every time a player kicks the ball. If it’s a soccer game I’d assume there’s multiple players

1 Like

The main reason it looks laggy is because by default the network ownership is handled automatically and switches between players. If you set the network owner to the server it might be delayed, but it shouldn’t stutter

2 Likes

Wait, is there a difference between setting the network owner to nil rather than just not setting the network owner to anything?

1 Like

Setting it to nil makes the server handle everything. I don’t think you can call it with no inputs though

2 Likes

it doesnt do anything (charssssssssss)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.