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?
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.
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.
@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.
--//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
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