CollectionService script sometimes not working

Like the title says I have this CollectionService script, sometimes it works, sometimes it doesn’t work, and I think it’s because the object/objects the script is affecting, hasn’t been loaded in yet, how could I wait for the object to load in first before the script affects the object?

local CollectionService = game:GetService("CollectionService")

local function AddPartFunctionality(ammobox)
	local Click = ammobox.ClickDetector
	
	local function Enabler(Player)
		--print("bngbdgh")

		--==================================================
		--Setting
		--==================================================
		local Ammo = math.huge --Amount of Ammo to give. Set it to "math.huge" to refill the gun's ammo.
		local GunToRefillAmmo = {
			"M9",
			"M9-S",
			"Ruger 22",
			"Full-Auto Ruger 22",
			"MAC-10",
			"M1911",
			--Add more gun here if you want
		}
		--==================================================

		local Enabled = true
		if Enabled and Player then
			local AmmoRefilled = false
			for _, GunName in pairs(GunToRefillAmmo) do
				local Gun = Player.Backpack:FindFirstChild(GunName) or Player.Character:FindFirstChild(GunName)
				if Gun then
					local GunScript = Gun:FindFirstChild("GunScript_Server")
					local Module = Gun:FindFirstChild("Setting")
					if GunScript and Module then
						local Module = require(Module)
						if GunScript.Ammo.Value < Module.MaxAmmo and Module.LimitedAmmoEnabled then
							Enabled = false
							AmmoRefilled = true
							local ChangedAmmo = (Ammo == math.huge or GunScript.Ammo.Value + Ammo >= Module.Ammo) and Module.MaxAmmo or (GunScript.Ammo.Value + Ammo)
							GunScript.Ammo.Value = ChangedAmmo
							GunScript.ChangeMagAndAmmo:FireClient(Player,Module.AmmoPerMag,ChangedAmmo,0)
						end
					end
				end
			end
			if AmmoRefilled then ammobox:Destroy() end
		end
	end

	Click.MouseClick:Connect(Enabler)
	
	Click.MouseClick:Connect(function()
		Enabler()
	end)
	--print("gvfdjkhfgjd")

	task.wait(120)
	ammobox:Destroy()
end

for i,v in pairs(CollectionService:GetTagged("LightAmmo")) do
	AddPartFunctionality(v)
end

CollectionService:GetInstanceAddedSignal("LightAmmo"):Connect(function(object)
	AddPartFunctionality(object):Wait(2)
end)

maybe try disabling workspace.StreamingEnabled

AddPartFunctionality(object):Wait(2)

delete :Wait(2)

Your AddPartFunctionality function does not return an RBXScriptSignal object or any sort of imitation of one. Even so, the Wait method does not allow you to specify a timeout. Your callback to GetInstanceAddedSignal callback should be raising an error. This is a different problem though. You’re also duplicating the activation of the Enabled function

also, try using this line to get the ClickDetector:

local Click = ammobox:WaitForChild("ClickDetector")

and you realize that you’re calling the function twice when the part is clicked, right?

I’m pretty sure that without it, it doesn’t work as often, or its just a coincidence, and I also forgot to delete “Click.MouseClick:Connect(Enabler)”.

I don’t think it makes a difference to your script because it errors after the function is called. try using :WaitForChild like I suggested above. are there any errors when it doesn’t work?

There are no errors when it doesn’t work, and it’s probably just a coincidence but using :WaitForChild on the clickdetector makes it work less

what do you mean when you say it works sometimes? does it only work on some parts, or does it only work some of the times you playtest?

Both, sometimes I can refill ammo on some of them, and all of the others you can’t refill from, or none of them work at all

how are you tagging the parts?

Tagging them with the tag editor plugin

what doesn’t work? is it just the ammo refill part, or do the print statements also not print? (remove the dashes)

If the ammo refill doesn’t work the prints don’t print

for i,v in pairs(CollectionService:GetTagged("LightAmmo")) do
	print("a")
	AddPartFunctionality(v)
end

CollectionService:GetInstanceAddedSignal("LightAmmo"):Connect(function(object)
	print("b")
	AddPartFunctionality(object):Wait(2)
end)

what prints?

A only prints, but the ammo never works, so I undashed “Click.MouseClick:Connect(Enabler)”, and it works more for some reason

That’s cause
image
your function yields the loop.

task.delay(120, ammobox.Destroy, ammobox)
1 Like

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