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)