Tagged.touched event not firing

I’m creating a collection system where the player can run through orbs and receive rewards however the .touched event seems to not be firing.

I have used the same/similar collection script for my other games and it worked fine for those, I’m not sure what is going wrong.

When adding an individual script under each orb the .touched event fires however when using the CollectionService it doesn’t fire.

As you can see here nothing is printed even though I’m touching the orb:

This is the collection script:

local CollectionService = game:GetService("CollectionService")
local taggedOrb = CollectionService:GetTagged('Orb')
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

function debounce(func)
	local isRunning = false
	return function(hit)
		if not isRunning then
			isRunning = true 

			func(hit)

			isRunning = false
		end
	end
end

for _, tagged in pairs(taggedOrb) do
	tagged.Touched:Connect(debounce(function(hit)
		print("test")
		if hit.Parent:FindFirstChild("Humanoid")then
			local character = hit.Parent
			local player = Players:GetPlayerFromCharacter(character)
			if player then
				local leaderstats = player.leaderstats
				local data = player.Data
				if tagged.Name == "small" then
					leaderstats.Power.Value = leaderstats.Power.Value + 3
					data.Gems.Value = data.Gems.Value + 1
				elseif tagged.Name == "medium" then
					leaderstats.Power.Value = leaderstats.Power.Value + 7
					data.Gems.Value = data.Gems.Value + 3
				elseif tagged.Name == "big" then
					leaderstats.Power.Value = leaderstats.Power.Value + 15
					data.Gems.Value = data.Gems.Value + 5
				elseif tagged.Name == "huge" then
					leaderstats.Power.Value = leaderstats.Power.Value + 30
					data.Gems.Value = data.Gems.Value + 10
				end
				tagged:Destroy()
			end
		end
	end))
end

Any suggestions would be greatly appreciated!

Hey! You should probably check for newly tagged instances using the CollectionService:GetInstanceAddedSignal and make sure the orbs get the tag during runtime.

tagged.Touched:Connect(debounce(function(hit)
This line is invalid, I’ll leave the explanation to someone else.

1 Like

To achieve the desired result here, you should change that line of code with

tagged.Touched:Connect(function(hit)
--debounce check goes here
--your code
end)
1 Like

Even without a debounce the tagged.Touched:Connect(function(hit) does not work.

I think I have found the issue
Why does that end have 2 brackets?

(debounce(function) adds another bracket at the end

Here is the updated code to detect for newly tagged instances and correctly detecting the touched event.

local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function onHit(hit)
    print("test")
    if hit.Parent:FindFirstChild("Humanoid")then
        local character = hit.Parent
        local player = Players:GetPlayerFromCharacter(character)
        if player then
            local leaderstats = player.leaderstats
            local data = player.Data
            if tagged.Name == "small" then
                leaderstats.Power.Value = leaderstats.Power.Value + 3
                data.Gems.Value = data.Gems.Value + 1
            elseif tagged.Name == "medium" then
                leaderstats.Power.Value = leaderstats.Power.Value + 7
                data.Gems.Value = data.Gems.Value + 3
            elseif tagged.Name == "big" then
                leaderstats.Power.Value = leaderstats.Power.Value + 15
                data.Gems.Value = data.Gems.Value + 5
            elseif tagged.Name == "huge" then
                leaderstats.Power.Value = leaderstats.Power.Value + 30
                data.Gems.Value = data.Gems.Value + 10
            end
            tagged:Destroy()
        end
    end
end

local function onInstanceAdded(instance)
    instance.Touched:Connect(onHit)
end

local taggedOrb = CollectionService:GetTagged('Orb')
for _, tagged in pairs(taggedOrb) do
	onInstanceAdded(tagged)
end

CollectionService:GetInstanceAddedSignal('Orb'):Connect(onInstanceAdded)

The orbs seem to be getting the tag

Yes but the other script goes through ones that already have their tag at the script’s runtime, therefore if they weren’t loaded in during script runtime they won’t have their touch connection setup.

1 Like