I need help with my code being laggy

I am writing this code for a superball, but if you throw this ball long enough, roblox will start to lag

can someone help and tell me what’s wrong with it?

tool = script.Parent

damage = 30

cd = 0.7

db = false

local idle 



tool.Equipped:Connect(function()

	local player = game.Players:GetPlayerFromCharacter(tool.Parent)
	if player then
		if player.Character then
			idle = player.Character.Humanoid:LoadAnimation(script.Idle)

		end
	end

	local player = game.Players:GetPlayerFromCharacter(tool.Parent)
	if player then
		if player.Character then
			local anim = player.Character.Humanoid:LoadAnimation(script.Equip)
			anim:Play()	
			task.wait(.3)
			idle:Play()	
		end
	end

	tool.Equip:Play()
end)

tool.Unequipped:Connect(function()

	local player = game.Players:GetPlayerFromCharacter(tool.Parent)
	if player then
		if player.Character then
			idle = player.Character.Humanoid:LoadAnimation(script.Idle)

		end
	end

	idle:Stop()


end)


tool.Activated:Connect(function()
	if db==true then return end
	db=true
	task.delay(cd,function()
		db=false
	end)

	local mousehit = game.ReplicatedStorage.GetMouse:InvokeClient(game.Players:GetPlayerFromCharacter(script.Parent.Parent))
	local mag = (tool.Handle.Position-mousehit.Position).Magnitude

	if mag>35 then mag=35 end

local ball = tool.Handle:Clone()
	ball.Parent = workspace.Debris
	ball.Velocity=mousehit.LookVector*(mag*2.5*1.25)+Vector3.new(0,50,0)

	ball.Position=tool.Handle.Position





tool.Throw:Play()



	local player = game.Players:GetPlayerFromCharacter(tool.Parent)
	if player then
		if player.Character then
			local	throw = player.Character.Humanoid:LoadAnimation(script.Throw)
			throw:Play()
		end
	end



	local HeartConn

	local function OnHeart()
		local tabla = workspace:GetPartsInPart(ball)

		local part = nil


		--tabla=ball:GetTouchingParts()





		local pos = ball.Position


		for i,v in pairs(tabla) do

			if not game.Players:GetPlayerFromCharacter(v.Parent) and not game.Players:GetPlayerFromCharacter(v.Parent.Parent) and v.Parent~=workspace.Debris  then

				part=v
				if part==nil then else

					ball.Position=pos
					--ball.Anchored=true
					HeartConn:Disconnect()
					task.wait(.15)

					ball.Position=pos


					ball.Anchored=true
					local tweenInfo = TweenInfo.new(
						.5, -- Time
						Enum.EasingStyle.Quad, -- EasingStyle
						Enum.EasingDirection.Out, -- EasingDirection
						0, -- RepeatCount (when less than zero the tween will loop indefinitely)
						false, -- Reverses (tween will reverse once reaching it's goal)
						0 -- DelayTime
					)



					local tween = game:GetService("TweenService"):Create(ball, tweenInfo, { Size=Vector3.new(3,3,3),Transparency=1 })

					tween:Play()
					game:GetService("Debris"):AddItem(ball,.5)

					if part.Parent:FindFirstChild("Humanoid") then
						part.Parent.Humanoid:TakeDamage(damage)

					end

break


				end


			end



		end


	
	end

	HeartConn = game:GetService("RunService").Heartbeat:Connect(OnHeart)

	task.delay(3,function()
		HeartConn:Disconnect()
	end)













end)
2 Likes

its probably the magnitude thats lagging it

1 Like

This is false, .Magnitude is indeed slow compare to others mathematicians equation but it is not that much to become noticeable .

Seem like the problem is you check collision and per part collision you play a tween service, in every frame, that will tank the performance when ran that much Tween

1 Like

but doesn’t it disconnect the heartconn , which will stop it from doing more than one tween?


in output it prints “hi” once, which would mean it does the tween once

i used GetPartBoundsInRadius instead and i noticed a lot less lag

1 Like

First of all, don’t store your server scripts inside tools, usually when larger server script is inside tool it lags as for replication, use one script for all tools and then send info via remote

What do you mean by lag? Is it FPS lag or server lag?

server lag. the touch will start to delay more and more before it does the tween

if the script above is server script, then it is normal to have such a lag, especially when you spam it because the amount of information transfer from client-server.

If you want it to be smooth and instant, not laggy then you have to rewrite your tool in client and handle things like Tool.Activated and client visual from there, then you will send the event to server script to handle damage. Finally, in the visual replication, you should send the info you need for visual to server, then from server to allClient expect the player throwing the ball, then do the visual from their client. It should give you a smooth, non laggy result with a bit of visual desync on the damage.