Hi everybody,
I’m following one of the official Roblox tutorial, “The Adventure Game Project” tutorial, that you can find at https://create.roblox.com/docs/education/adventure-game-series/landing
I’m following it exactly step-by-step but I get an error on the third part about collecting items Collecting Items | Roblox Creator Documentation
The source code that causes problem is this:
local toolPart = script.Parent
local tool = toolPart.Parent
local function onTouch(partTouched)
local canHarvest = partTouched:FindFirstChild("CanHarvest")
if canHarvest then
print("Found an item")
end
end
tool.Touched:Connect(onTouch)
because when I run the game I get:
Touched is not a valid member of Backpack
Now, reading the source and the instruction on the site you can notice that “tool” variable mistakenly points to Backpack (since it is the parent of the parent of “toolPart”) that does not have a Touched event. Neither using the “Handle” or directly the StarterTool for attaching the Touched event solved the issue.
Any advice? Thanks
Mind showing the layout of the tool and where the script is where you are running it. From looking at the error I am guessing the “tool” variable is pointing towards the Backpack rather then the tool.
You might be able to just remove the “Parent” from the toolPart.Parent
however without checking how your tool is located and where the script is located I cannot be 100% sure.
Try this
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local function onTouch(partTouched)
local canHarvest = partTouched:FindFirstChild("CanHarvest")
if canHarvest then
print("Found an item")
end
end
Handle.Touched:Connect(onTouch)
Thanks for your reply. I’m following exactly step-by-step the official tutorial. I post a screenshot of the Roblox site of what I did:
I still get that error even when I point directly to the tool.
Bye
Thanks for your reply. I did before what you suggested but I still got the same error: even if I point directly to the tool it seems that it has not a Touched event. Bye
I think that the code published on the Robox website is buggy and you can found it here: Collecting Items | Roblox Creator Documentation
I fixed it in the follwing manner and now stuff works:
local toolPart = script.Parent -- it points to StarterTool
--local tool = toolPart.Parent This is useless and causes issues
--local backpack = tool.Parent
--local player = backpack.Parent
local backpack = toolPart.Parent
local player = backpack.Parent
local playerStats = player:FindFirstChild("leaderstats")
local playerItems = playerStats:FindFirstChild("Items")
local playerSpaces = playerStats:FindFirstChild("Spaces")
local function onTouch(partTouched)
--local canHarvest = partTouched:FindFirstChild("CanHarvest") This is incorrect
local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest")
if canHarvest then
if canHarvest.Value == true and playerItems.Value < playerSpaces.Value then
playerItems.Value = playerItems.Value + 1
canHarvest.Value = false
-- Reset partTouched, the harvested item
partTouched.Transparency = 1
partTouched.CanCollide = false
wait(5)
-- Make the harvested item reappear and usable again
canHarvest.Value = true
partTouched.Transparency = 0
partTouched.CanCollide = true
end
end
end
--toolPart.Touched:Connect(onTouch) This is incorrect
toolPart.Handle.Touched:Connect(onTouch)
This code goes into a script called ToolScript
and put under StarterPack\StarterTool
as indicated in the tutorial instructions.
Now the only thing that can be improved (“fixed” I can say) is that at the moment it is sufficient to have the tool in the hand and touched the crystal for getting it but it could be nice to have to use the mouse button for doing that.