Touch event not detecting for a folder of models

Hello, I want to make a system where you can collect cloning bills around the map and get money in leader stat value.

The issue is that the touch event does not detect the primary part in the model.
Here is my script:

local player = script.Parent -- The script is in a starting player script
local Money = game.Workspace.Money -- Money folder

for _,v in pairs(Money:GetChildren()) do
	if v:IsA("Bill") then
		v:FindFirstChild("Part").Touched:Connect(function(hit) -- only 1 part named "Part"
			print("touched") -- Doesnt print
		end)
	end
end

I have looked through many solutions on the developer forum but i couldn’t find any. Does anyone know what could be causing this?

3 Likes

Firstly, ServerScripts do not run if placed in the StarterPlayerScript, so make sure it’s a LocalScript.

Secondly, I don’t think local player = script.Parent works if the script is placed inside of the StarterPlayerScript, so use local player = game.Players.LocalPlayer instead.

Lastly, what’s a “Bill”? When you use :IsA(), make sure you refer to the ClassName of an object and not the Name, for example: “Part”, “MeshPart”, “BillboardGui” … I would guess “Bill” is supposed to be the name of the parts so you should do if v.Name == "Bill" then.

1 Like
local player = script.Parent -- The script is in a starting player script
local Money = game.Workspace.Money -- Money folder

for _,v in pairs(Money:GetChildren()) do
	if v:IsA("Model") then -- Check if it's a model
		for _, part in pairs(v:GetChildren()) do
			if part:IsA("BasePart") then -- Check if it's a part
				part.Touched:Connect(function(hit)
					print("touched") -- Should print now
				end)
			end
		end
	end
end

Also, make sure that the parts are not anchored and that their CanCollide property is set to true, as the Touched event will not fire for anchored parts or parts with CanCollide set to false.

Sorry i forgot to mention that that script in the player scripts is a local script
“Bill” is a model

1 Like

I used prints to figure out that the model is being detected but not detecting a base part.
Here is what it looks like in the explorer tab.

2023-11-04 (1)

“Part” Being the one that detects touch.

1 Like

The money is unanchored and has collide turned on

1 Like

@Youf_Dev explained it fairly well. Bill isn’t a valid class in Roblox. You have to do a name comparison instead.

Code:

local player = script.Parent -- The script is in a starting player script
local Money = workspace.Money -- Money folder

local function InitializeBill(model)
	local part = model.Part
	
	model.Touched:Connect(function(hit) -- only 1 part named "Part"
		print("touched") -- Doesnt print
	end)
	
	--//Do whatever you want
end

Money.ChildAdded:Connect(function(child)
	if child.Name == "Bill" then
		InitializeBill(child)
	end
end)

for _, v in Money:GetChildren() do
	if v.Name == "Bill" then
		task.spawn(InitializeBill, v)
	end
end

I also added code to detect if any Bills are added.

It says Part is not a valid member of Model “Workspace.Money.Bill” - Client - Collect money:5

I forgot to add a :WaitForChild.

Code:

--//Variables
local player = script.Parent -- The script is in a starting player script
local Money = workspace.Money -- Money folder

--//Functions
local function InitializeBill(model)
	local part = model:WaitForChild("Part")
	
	model.Touched:Connect(function(hit) -- only 1 part named "Part"
		print("touched") -- Doesnt print
	end)
	
	--//Do whatever you want
end

Money.ChildAdded:Connect(function(child)
	if child.Name == "Bill" then
		InitializeBill(child)
	end
end)

for _, v in Money:GetChildren() do
	if v.Name == "Bill" then
		task.spawn(InitializeBill, v)
	end
end

It now says Touched is not a valid member of Model “Workspace.Money.Bill” - Client - Collect money:9 which is an error code i get alot, but dont know how to fix

I figured out it needs to be a base part for the touched event to work

Ohhh wait, I accidentally did Model.Touched instead of Part.Touched. By the way, parts ARE a BasePart. BaseParts include Parts, Meshes, and some other things.

Code:

--//Variables
local player = script.Parent -- The script is in a starting player script
local Money = workspace.Money -- Money folder

--//Functions
local function InitializeBill(model)
	local part = model:WaitForChild("Part")
	
	part.Touched:Connect(function(hit) -- only 1 part named "Part"
		print("touched") -- Doesnt print
	end)
	
	--//Do whatever you want
end

Money.ChildAdded:Connect(function(child)
	if child.Name == "Bill" then
		InitializeBill(child)
	end
end)

for _, v in Money:GetChildren() do
	if v.Name == "Bill" then
		task.spawn(InitializeBill, v)
	end
end

@STK_gold1

sorry it took me so long to respond, i will test this out.

the problem seems to lie in the

Money.ChildAdded:Connect(function(child)
	print("something added")
	if child.Name == "Bill" then
		print("bill added")
		InitializeBill(child)
	end
end)

none of the prints i added seem to print anything. Do you know if there is something wrong in this code?

There is nothing wrong with this specific code, but it appears that a child is never being added to ‘Money’.

Oh you are right, when I put the original money model in the folder script, I forgot to make the model duplicate into the folder :man_facepalming:

Thank you and everyone else that helped.

1 Like

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