How can I fix lag in my game?

Hello everyone! I’ve recently made a game and I’m ready to release it, but there’s one problem I’ve been encountering. In the game, you can gain loads of new pets and items. But, if you gain too many then it will start to lag and become unplayable. Here is a video to show what is going on:

External Media

How it is supposed to be like:

1 Like

Are you running those coins, pets etc. in the server side? If you do it on the server side it clogs network bandwith. Try rendering it on Client side, maybe it could help

2 Likes

I have tried that, but that did not work.

Are the Coins and Pets there Unanchored?

No. All of the pets, coins, and parts are anchored

Are you controlling them via a for loop where you loop through all the items? If so you can try multithreading

Each pet has its own script and checks if “AutoClicker” is on. If autoclicker is on then it will start spawning in blocks every couple of seconds at the pets position. This action is controlled in a while loop. Is having multiple scripts the cause?

Here is a snippet of the code used in each pet script:

task.spawn(function()
	while wait(2) do
		local Data = require(Owner:WaitForChild("PlayerData"))
		if MarketplaceService:UserOwnsGamePassAsync(Owner.UserId, AutoclickerId) and Data.AutoClickerToggled then
             -- block spawning code
        end
	end
end)

You shouldn’t get the player data every 2 second and check if it’s eligible to spawn the blocks that way.

Save the result of the check once, for example as an attribute on the Player instance, called let’s say “HasAutoclicker”

Also I’d suggest making a centralized system for processing AI of the pets. I’d suggest looking into Tags for efficient direct access to specific group of tagged AIs.

Link: CollectionService | Documentation - Roblox Creator Hub

Tip: You could also use AddTag and RemoveTag directly on the instance, instead of thru CollectionService

1 Like

The reason I have it set up like that is because the data changes, and if I get it once, it will stay the same and won’t update. (Correct me if I’m wrong)

Well usually in most games if the player buys a gamepass they need to rejoin. But you could do so it runs the checks after the player purchased the gamepass and apply the corresponding attributes.

Isn’t that worse than the way I’m doing it? Sorry, I didn’t really get it—could you explain it a bit more?

No, it’s not worse. It applies an attribute on the player once, depending if it initially found a gamepass in possession or when a player purchased one.

The way you’ve done it constantly checks if a player has a gamepass every 2 seconds, which isn’t needed at all. You just have to save the result once, as I’ve suggested using an attribute.

Oh, I understand now! I suppose that does help with reducing lag. I’ll give your tag method a try—really appreciate the support!

1 Like

bruh all coin effects like this should always be client sided.

also you want to probably use task.defer instead of task.spawn. And why are you requiring the data every 2 seconds? I agree with @VioletElementalist – you need to control all the pets in a single script. You should probably take a look into collection service.

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