How can I reduce lag with my script? It's ruining my gameplay!

My coin system may be the source of the lag as it contains a lot of problems. For starters, it tries to handle every player collecting coins and generates them even if millions of studs away. Im unsure how to approach this laggy code:

local Coin = {}

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

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local Tokens = workspace:FindFirstChild("Tokens")
local CoinFolder = ServerStorage:FindFirstChild("Coins")

local CollectionService = game:GetService("CollectionService")

local LS = nil

Coin.CoinTaken = function(player, CoinType, TheCoin, state)

	if not state then
		state = true

		local Amount = 0

		local PlayerToFind = Players:GetPlayerFromCharacter(player.Parent)

		if not PlayerToFind then return end

		LS = PlayerToFind:FindFirstChild("leaderstats")

		if TheCoin and TheCoin:FindFirstChild("Amount") and LS then
			if MarketplaceService:UserOwnsGamePassAsync(PlayerToFind.UserId, 147063459) then
				LS.Tokens.Value += TheCoin.Amount.Value * 2
				LS.Tokens.Value += TheCoin.Amount.Value

		Amount = 5

		if TheCoin:FindFirstChild("Timer") then
			Amount = TheCoin.Timer.Value

		local PreviousPos = TheCoin.Position

		-- WAIT 5 seconds to spawn that coin again
		task.delay(Amount, function()
			Coin.New(PreviousPos, CoinType)
			state = true

Coin.New = function(position, Type)
	local desiredCoin = CoinFolder:FindFirstChild(Type)
	local NewCoin = desiredCoin:Clone()

	CollectionService:AddTag(NewCoin, "Coin")

	NewCoin.Parent = Tokens
	NewCoin.Position = position

		Coin.CoinTaken(player, Type, NewCoin, false)

return Coin


ideas i have are:

would it be possible to make it client sided to reduce even more lag?
would it be possible to give it a distance range then?

its really confusing on how to start making this script less laggy.

Use a tween for coin movement. The sudden movement change can help decrease lag. Try to use a debounce to prevent players from collecting coins too quickly. You could also decrease the parts or triangles of the current coin model. Maybe even decrease the size. Also you can just generate coins when players are nearby instead of millions of studs away.

Try to store the player owning the gamepass as a variable and not check it every .Touched event.

Also, no reason to use FindFirstChild.

How would I attempt to store the gamepass data?

and to add on to it, since its a server script. I’m unsure how to get the player into the parameters which is confusing for me.


First thing that comes to my mind is checking when the player joins and setting an attribute to that player.

1 Like

it also likes to serve this error, in which there is a amount value, but it may be deleted too fast?

also if i leave the game thats because it kicks me if playing in studio and in game at the same time


Here’s a place with my take on the coins.
ImprovedCoins.rbxl (44.1 KB)

1 Like

Don’t run animations on the server lol, run them on every client instead.

1 Like

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