My Script Lags When I First Use It

When I first use my script it will ignore the waits. I dont know why it does this can someone please help me.

It does this in studio and in the real game.

The script is in local script form but it still does the same in scripts.

local Player = game.Players.LocalPlayer
local Character = Player.Character

local Mouse = Player:GetMouse()

local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

Mouse.Button1Down:Connect(function()
    local Character = Player.Character

    local effectFolder = game.Workspace:FindFirstChild(Player.Name.." Effects") or Instance.new("Folder", workspace)
    effectFolder.Name = Player.Name.." Effects"

    local JP = Character.Humanoid:LoadAnimation(script.JP)
    local RP = Character.Humanoid:LoadAnimation(script.RP)
    local JPL = Character.Humanoid:LoadAnimation(script.JPL)

    Character.HumanoidRootPart.Anchored = true

    JP:Play()
    wait(JP.Length)
    JPL:Play()

    local Arm = script.SmoothCube:Clone()

    Debris:AddItem(Arm, 4)

    Arm.BrickColor = Character["Right Arm"].BrickColor
    Arm.Size = Character["Right Arm"].Size + Vector3.new(.01, .01, .01)
    Arm.CFrame = Character["Right Arm"].CFrame * CFrame.new(0, -1.5, 0)
    Arm.Parent = effectFolder

    TweenService:Create(Arm, TweenInfo.new(2.5, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Size = Arm.Size + Vector3.new(0, 100, 0), CFrame = Arm.CFrame * CFrame.new(0, -50, 0)}):Play()
    wait(2.5)   
    TweenService:Create(Arm, TweenInfo.new(1.5, Enum.EasingStyle.Quart, Enum.EasingDirection.In), {Size = Arm.Size + Vector3.new(0, -102, 0), CFrame = Arm.CFrame * CFrame.new(0, 51.5, 0)}):Play()
    wait(1.5)

    for i,v in pairs(Character.Humanoid:GetPlayingAnimationTracks()) do
        if v.Name == "JPL" then
            v:Stop()
        end
    end

    RP:Play()
    Character.HumanoidRootPart.Anchored = false
end)
3 Likes

Provide script, we cant help you without it.

Sorry about, I just posted the script.

How many items fo you have under workspace? You are checking every single one before finally creating the folder:

    local effectFolder = game.Workspace:FindFirstChild(Player.Name.." Effects") or Instance.new("Folder", workspace)
    effectFolder.Name = Player.Name.." Effects"

Why not just create the Effects folder in Studio so you know it exists. I epcet that would stop the lag seen

Ill see if that fixes the problem.

I made it just create a folder and it still didnt work, then i put a already made folder in workspace and it still didnt work.

Try doing (“Control + Shift + F5”) What FPS are you getting when it starts lagging?

Also this might have something to do with it?

 Debris:AddItem(Arm, 4)

When you added the item, did you remove these lines:

    local effectFolder = game.Workspace:FindFirstChild(Player.Name.." Effects") or Instance.new("Folder", workspace)
    effectFolder.Name = Player.Name.." Effects"

then add the following near the top of the script, but OUTSIDE of the Mouse.Button1Down:Connect(function()

local effectFolder = Instance.new("Folder")
effectFolder .Name = Player.Name.." Effects"
effectFolder .Parent = workspace

yeah i did and it didnt fix the problem

Check workspace, if it has repeated arms then that’s the problem and you can deal with it yourself.

The problem was solved and its becuause i use wait(AnimationTrack.Length), and an animation must run before it can get the length so it got the length as 0, I used AnimationTrack.Stopped:Wait()