Cash Collects on Singleplayer Playtest, but doesn't work on Multiplayer Playtest

So I’m having an issue with this cash collection system I made, as what is mentioned in the title, in single-player, the cash collectible can be collected normally, but in multi-player, neither players can collect the cash

local ColSer = game:GetService("CollectionService")
local Players = game:GetService("Players")
local db = false

local function TheFunc(obj)
	
	obj.Touched:Connect(function(toucher)
		if db == true then return end
		db = true

		local plr = Players:GetPlayerFromCharacter(toucher.Parent)
		
		if plr then
			local Bag = Players[plr.Name].Bag
			local Cash : IntValue = Bag.Cash

			print("Hit")
			Cash.Value += 15 

			obj:Destroy()

			wait(0.1)

			db = false
		end

	end)
end

for i, v : Part in next, ColSer:GetTagged("Cash") do

	TheFunc(v)

end

ColSer:GetInstanceAddedSignal("Cash"):Connect(function(obj)
	
	for i, v : Part in next, ColSer:GetTagged("Cash") do

		TheFunc(v)

	end
	
	TheFunc(obj)
end)
	

Mind you, this is the only script that detects if a player collects a cash object

1 Like

When the object gets touched by any part the devounce is set to true, this can cause a problem if the toucher isn’t a player since db wont be set back to false

2 Likes

I think I overlooked it so hard that I didn’t notice that this was the problem.

The reason why I added the debounce is because the script activates more than once when touched, any suggestions on how to fix it?

1 Like

Move this inside the TheFunc function. All your parts share the same debounce variable so if one’s touched all of them won’t work until the debounce is false. Also, why do you call the function over and over again in your GetInstanceAddedSignal? Just call the function once. Also, you do not need to loop through all the tagged instances

You can set debounce to true after checking if plr exists

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