Confused On Something That Has To Do With a Item Collecting Script | Scripting Support

Hi there. So I’m a bit confused on why I have to include “Cupcake” on one line of code of my script. I was following a tutorial on developer.roblox.com and for some reason, when I use the code they have typed out, my script does not work. Though when I did one small tweak to the script, it worked and I’m confused why it worked when I wrote “Cupcake” on that line of code in my script.

What I’m trying to do is basically when I touch a item with an object, I should have something print in my output.

Here’s the code the tutorial has typed out:


 -- Gives players items when tool hits cupcakes
local tool = script.Parent
local scoop = tool.Scoop

local function onTouch(partTouched)
    print("ok so far so good")
    print(partTouched.Parent)
    local  bool = partTouched.Parent.Cupcake:FindFirstChild("yes") --- I should get "yes" when I do the statement print(bool)
    print(bool)
    if bool then
        print("Found an item. This is a report to the output")
    end
end
 
scoop.Touched:Connect(onTouch)

So then, my script worked. Why do I have to type “Cupcake” on line 8 of my script? On the Roblox Developer Hub, their script worked without having to put the “Cupcake” on the line of code.


So I’m not sure why “Cupcake” needs to be said in the script and I’m also not sure how the script in the Roblox Developer Hub tutorial is not working for me, but worked for them.


Here’s the link to the tutorial I used for creating the script:
https://education.roblox.com/en-us/resources/adventure-game-creating-tools


Here’s also a video recording I did regarding this:


robloxapp-20200824-2109470.wmv (677.3 KB)


Thank you and have a great rest of your day. :slight_smile:

Did you make sure that if you change the cupcake to say a banana, that you change the cupcake model’s name to banana also?

Yes, I did. I’m just not sure why “Cupcake” needs to be said in line 8 of the script.

Is there a bool value parented under that “cupcake model”?

Yes, there is. Here’s a screenshot of the BoolValue inside the part in Workspace:


image

Change that part named “Cupcake” to something like Banana.

Then use this script:

local tool = script.Parent
local scoop = tool.Scoop

local function onTouch(partTouched)
    print("ok so far so good")
    print(partTouched.Parent)
    local  bool = partTouched.Parent.Banana:FindFirstChild("yes") --- I should get "yes" when I do the statement print(bool)
    print(bool)
    if bool then
        print("Found an item. This is a report to the output")
    end
end

Actually just did that. Though I’m not understanding why I have to put something like “Banana” or “Cupcake” there. In the Roblox Developer Hub tutorial I followed, they didn’t put something in like “Cupcake” or “Banana” in line 8 of their script.

You have to change both the cupcake part’s name and the Cupcake in the script, line 8.

Yup, I’ve done that already. Though my question is, why do I have to put something like “Banana” or “Cupcake” in line 8 of the script?

1 Like

Well, you don’t have to. Try changing both of them to whatever you want.

What I meant was like why do I need to put those values in the script?

The “Cupcake” in line 8 has to be changed to the exact name as the part named “Cupcake”.

Yeah, I know. I’ve done that already.

Those don’t have to be bananas or cupcakes. They can be whatever you want.

I still don’t think you are understanding what I am saying. What I am saying is that in the tutorial I followed, they did the script differently.

Here’s the tutorial code:


local function onTouch(partTouched)
	local canHarvest = partTouched.Parent:FindFirstChild("CanHarvest")
	if canHarvest then
		print("Found an item")
	end
end
 
scoop.Touched:Connect(onTouch)

As you can see, they didn’t put the part name in the line I did. I put the part name, but in the tutorial, they didn’t.


So my question is, why I do I have to put the part name at line 8 of the script?

You don’t have to.

for i,v in pairs(tool:GetChildren())do
v.Touched:Connect(function(hit)
if hit:FindFirstChild("Harvestable") then
-- code
end)
end

Make sure that all the harvestable items have a object named “Harvestable”.

Okay, so then I would put my print statement after the if statement, correct? Also, there is a parsing error for the script.


image


Took of the parenthese on the “end” but then I got this:


image

My bad, add an end before the end of the touched function.

Sorry for the late reply. I used the same tutorial, and my version is working. I have a few suggestions if you are still having trouble.

Are you sure you have the script in the right place?

My Explorer looks like this:

image

The ToolScript is inside the FishingRod (my proxy for Scoop), not the StarterPack.

-- Gives players items when tool hits fish
local tool = script.Parent
local rod = tool.Rod

---I snipped some leaderboard code out from here

local function onTouch(partTouched)
	local canFish = partTouched.Parent:FindFirstChild("CanFish") ----note, no equivalent of 'Cupcake' is use for partTouched. canFish is what I called my bool in the script, CanFish is it's name inside the fish model. Your bool in the harvestable model needs to be called 'yes' to work.
	if canFish then
		-- Used for testing if code works
		print ("found a fish")
--- snipped out some code for hiding caught fish and updating leaderboard

	end
end

rod.Touched:Connect (onTouch)

This is what my harvestable models look like in workspace:
image
note the parts are unioned inside the model. Makes sure the ‘value’ box in the properties of your bool (which I think you have called ‘yes’ inside the harvestable model) is ticked.

Hopefully my code snips didn’t cut out anything important, and these questions and examples have helped you. If you already solved this, then hopefully my post has helped anyone else struggling with this tutorial.

1 Like