Here is a Gems script (overloading)

i dont know why sometimes it can not be collected, maybe there are too many people in the same server or the script is overloading or what ? Is there anyways that I can fix it ?

here is the gems script (located in ServerScriptService) it handle all the Gems

-- Alternative implementation
local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteFolder = ReplicatedStorage:WaitForChild("GemsGuiRemote")
local GemsValue = 1
--
local debounceGems = {} -- Dictionary of gem-objects that are currently in 'debounce'

--
local function GemHideThenReappear(gem)
	--gem.Position = Vector3.new(-195.806, 1.322, -189.686)

	wait(5)

	-- Reappear			
	--local pos1 = math.random(1340.551, 1409.617)
	--local pos2 = math.random(501.232, 501.232)
	--local pos3 = math.random(81.194, 117.067)
	--gem.Position = Vector3.new(pos1,pos2,pos3)
	gem.Sparkles.Enabled = true

	local tween = TweenService:Create(gem, TweenInfo.new(0.5), { Transparency = 0 } )
	
	-- Once tween has completed, then un-debounce the object
	tween.Completed:Connect(function()
		debounceGems[gem] = nil
	end)
	
	tween:Play()
end

--
local function GemTouched(gem, hit)
	if not debounceGems[gem] then
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if player ~= nil then
			RemoteFolder.ShowGems:FireClient(player,GemsValue)
			-- Set debounce for only this particular object
			debounceGems[gem] = true
			
			local PlayerBackpackCapacity = hit.Parent:FindFirstChild("Backpack").capacity
			
			if player.leaderstats.Gems.Value + GemsValue >= PlayerBackpackCapacity.Value then
				player.TotalGems.Value = player.TotalGems.Value + (PlayerBackpackCapacity.Value - player.leaderstats.Gems.Value)
			else
				player.TotalGems.Value = player.TotalGems.Value + GemsValue
			end
			
			player.leaderstats.Gems.Value = player.leaderstats.Gems.Value + GemsValue
			
			gem.GemsSound:Play()
			gem.Sparkles.Enabled = false
			
			local tween = TweenService:Create(gem, TweenInfo.new(0.5), { Transparency = 1 } )
			
			-- Once tween has completed, then hide the object 
			tween.Completed:Connect(function()
				GemHideThenReappear(gem)
			end)
			
			tween:Play()
		end
	end
end

--
for _,gem in ipairs(CollectionService:GetTagged("MainIslandGems")) do
	gem.Touched:Connect(function(hit)
		-- Need to supply the `GemTouched`-function with what `gem`-object that is actually being touched
		GemTouched(gem, hit)
	end)	
end
2 Likes

Well one observation I made is this:

local PlayerBackpackCapacity = hit.Parent:FindFirstChild("Backpack")

Should this not be:

local PlayerBackpackCapacity = player:FindFirstChild("Backpack") -- as defined earlier

Also, your script doesn’t check if whats been hit actually is a descendant of the players character - just if it is a child of the players character. You can easily do this with hit:IsDescendantOf(player.Character) when cycling through players.

2 Likes

its still like that once the server is created for a long time

Have you actually done any testing of it yourself? From a glance it is difficult to say where the issue is.

It might possibly be your debounce.

1 Like

yes i did i test so many time by myself

What tests have you done?

I obviously can’t work miracles so I’d need to know what tests you’ve done and what the conclusions were.