Coin Piles not being collected on MouseClick

I am trying to make a single script (ServerScriptService) that when you click a coin pile (in my case a haybale) you receive some hay. No errors are occurring but it is not working at all. I don’t know what’s wrong. Before, I had a script under each individual hay bale on the map. It was working but is not convenient and when making changes will be very tedious. Plus I think some performance issues may arise in future maps???

*Old Script

local db = false 
local hay = script.Parent
local clickDetector = hay.ClickDetector

clickDetector.MouseClick:Connect(function(player)
	if not db then
		db = true
		hay.Transparency = 1
		local leaderstats = player:FindFirstChild("leaderstats")
		local hay  = leaderstats:FindFirstChild("Hay")
		hay.Value = hay.Value + 50
	end
	wait(1.5)
	hay.Transparency = 0
	
	hay.Position = Vector3.new(math.random(69,186), 3, math.random(-171,-41))
	db = false
end)

*New Script

local db = false 
local hay = game.Workspace.HayLocations:GetChildren("Hay")
if hay then
	local clickDetector = hay.ClickDetector 
	if clickDetector then
		clickDetector.MouseClick:Connect(function(player)
			if not db then
				db = true
				hay.Transparency = 1
				local leaderstats = player:FindFirstChild("leaderstats")
				local hay  = leaderstats:FindFirstChild("Hay")
				hay.Value = hay.Value + 50
			end
			wait(1.5)
			hay.Transparency = 0
			hay.Position = Vector3.new(math.random(69,186), 3, math.random(-171,-41))
			db = false
		end)
	end
end

make sure that hay.CanQuery = true

maybe do

for i, hay in pairs(game.Workspace.Haylocations:GetChildren()) do
    local db = false 
    local clickDetector = hay.ClickDetector

    clickDetector.MouseClick:Connect(function(player)
	if not db then
		db = true
		hay.Transparency = 1
		local leaderstats = player:FindFirstChild("leaderstats")
		local hay  = leaderstats:FindFirstChild("Hay")
		hay.Value = hay.Value + 50
	end
	wait(1.5)
	hay.Transparency = 0
	
	hay.Position = Vector3.new(math.random(69,186), 3, math.random(-171,-41))
	db = false
end)
end
1 Like
local db = false 
local hay = script.Parent
local clickDetector = hay:FindFirstChild("ClickDetector")

clickDetector.MouseClick:Connect(function(player)
   local leaderstats = player:FindFirstChild("leaderstats")
   local hay  = leaderstats:FindFirstChild("Hay")
	if not db then
		db = true
		hay.Transparency = 1
		hay.Value += 50
       task.wait(1.5)
	    hay.Transparency = 0
	    hay.Position = Vector3.new(math.random(69,186), 3, math.random(-171,-41))
	    db = false
	end
end)

What does CanQuery do exactly?

It works! Thanks a lot! I’m not quite sure why it works though? Could you clarify please?

basically, all I did was I took your old code, but instead having it in each hay part I made a loop that went through the workspace.Haylocations then had it basically write the click detector code for each child of the workspace.Haylocations.

1 Like

if its off the part will not be able to be interacted with the mouse. basically to the mouse the part will be non existent

Oh alright I get it! Thanks again!