Candy/coin pickup system drops fps every time a new candy is made or picked up/collected

So, I’m currently making a coin pickup system for my halloween game, however there seems to be a problem.

After I’ve collected around 20 or so coins, my fps drops every time I pick up a “coin” and whenever a new one is made. I’ve tried everything I could think of to reduce anything that might cause low fps, but so far, nothing. Any help would be appreciated!

Code (server sided) (ignore the comments):

Password: KTU473uip2
Link: https://pastebin.com/MNFTBD17

You´re script is intensly hard to execute. I have seen various coroutine.wrap functions and the complexities with the tables nearly makes this look like it was designed to drop FPS. Instead of using a table to store data, try creating a folder in the script and using Instance.new() and make a boolean or IntValue or whatever you need and try using that. You need to understand how complex and how much the game has to work to do this much.

1 Like

What else causes FPS drop? Trying to edit some things away that I won’t be needing in my script for it to work.

What are some other functions that I can use instead of coroutine? Is binding a function to Heartbeat a good alternative?

The goal is not to eliminate coroutine. The goal is to make the scripts use it at least as possible and efficiently. And yes, heartbeats can help with the lag. If you do need to replace coroutine, you can use spawn() but I wouldnt highly recommend it.

1 Like

Could you list some functions that aren’t efficient and can be replaced by more efficient functions?

local que = {}
 
local function addCandy(plr,amt)
    local insertData = {
        plr = plr,
        amt = amt
    }
    
    table.insert(que,insertData)
end

Instead of this, use a folder in the script and use values such as a boolean or IntValue and store data using that.

1 Like

Why would using actual instances rather than tables be more efficient? Doesn’t it require more memory to create instances rather than using tables which can be required in one swift manner?

You have a point, however you have to realize he is inserting data into another table. At this point, it gets really hard to understand the lag each solution causes.

local candyAddQue = coroutine.wrap(function()
    while true do
        wait(.1)
        for index, queData in pairs(que) do
            local plr = queData.plr
            local amt = queData.amt
            
            DS2Handler("Increment","candycoins",plr,amt)
            candyEvent:FireClient(plr,"addCoins",amt)   
            
            table.remove(que,index)
        end
    end
end)()

I am not completely sure what this is for, however, if you are trying to remove a value at the same time, this will add to script lag. Assuming if you are just checking for something, just use a and statement before adding the values to the table. This should help with lag at great values.

So by removing that it should help with the lag? I’ve already done edits on that and changed it to just call the function with DS2 directly.

What does that function do? You need to describe it so I understand if you should and would remove it or not.

I made a DS2 handler, which pretty much does all the combining and everything in one module. I’ll send the code to you rq, please wait