How can i look for a handle in a folder that contains parts with a handle

Hello im a new scripter trying to learn the basics and im currently trying to find the handle that’s in a Apple but that apple is in a folder in Replicated Storage. I cant seem to get a .Touched function going though…

Here’s some insight of what i want to do. I want the apples Anchored property to be false when a player touches it.

when i try to do that myself i cant seem to grasp it. The apples are in a folder called Appletree Fruits in Replicated Storage. I also want to add it in the SpawnApple function so it funs everytime the funciton is called
can anyone help?

local RS = game:GetService("ReplicatedStorage")
local AppleTree = game.Workspace.AppleTree:WaitForChild("Fruits")
local ApplesClone1 = RS.AppleTreeFruits:GetChildren()
local SpecialFruits = RS.AppleTreeFruits:FindFirstChild("SpecialFruits"):GetChildren()

local function SpawnApple()
	wait(0.3)
	for i = 1, 42 do
		local AppleClone = ApplesClone1[i]:Clone()
		AppleClone.Parent = AppleTree
	end
end

for count = 1, 1, 1 do
	SpawnApple()
end

After creating a clone of the apple, you can reference the AppleClone variable and then look through the object for an object named “Handle”.

If the Handle is directly within the Apple, you could use Instance:FindFirstChild() on it, like so:

local AppleClone = ApplesClone1[i]:Clone()
local Handle = AppleClone:FindFirstChild("Handle")

Since you now have a reference to the Handle, you can connect the Touched event to a function so that it’ll run the desired code whenever something touches the Handle of the Apple:

local function AppleTouched(otherPart)
    -- Continue
end

local function SpawnApple()
    for i = 1, 42 do
        local Apple = ApplesClone1[i]
        local AppleClone = Apple:Clone()

        local Handle = AppleClone:FindFirstChild("Handle")
        if Handle then
            print("Handle exists for Apple #"..i)
            Handle.Touched:Connect(AppleTouched)
        end

        AppleClone.Parent = AppleTree
    end
end

Edit

Just re-read the post and realized you want to set the Anchored property to false when the Apple is touched by a player. In that case, the function activated by the Touch event would need to be revised… give me a minute :slight_smile:


In order to check if a player’s Character touched the Handle, we can use the Players:GetPlayerFromCharacter() method, as that will ensure that the script only unanchors the Handle if the Touched event was activated because of a player and not something else in the game.

For reference, when the Touched event is fired and connected to a function, the value it passes through into the parentheses of the function is the object that touched the Handle to activate the event. If a Character model activated the event, then it could be one of the Character’s arms, torso, etc., but because we need to reference the Character model itself, we can use Instance:FindFirstAncestorOfClass() on the object that touched the Handle. From there, we can check if that model is the Character of a player, and if it is, we can set the Anchored property of the Handle to false.

local Players = game:GetService("Players")
-- Reference to Players service at the top of the script

...

if Handle then
    print("Handle exists for Apple #"..i)

    Handle.Touched:Connect(function(otherPart)
        local model = otherPart:FindFirstAncestorOfClass("Model")

        if model then
            local player = Players:GetPlayerFromCharacter(model)

            if player then
                Handle.Anchored = false
            end

        end

    end)
end

There’s additional changes that could be made to this to refine it further, such as disconnecting the Touched event once the Handle has already been unanchored, or using if not [condition] return end to make the function activated by the Touched event a bit cleaner, but I’ll keep it simple for now. If you’d like for me to explain anything else, please let me know :smiley:

Here’s what a full revision of that function might look like:

local function SpawnApple()
    for i = 1, 42 do
        local Apple = ApplesClone1[i]
        local AppleClone = Apple:Clone()

        local Handle = AppleClone:FindFirstChild("Handle")
        if Handle then
            print("Handle exists for Apple #"..i)

            Handle.Touched:Connect(function(otherPart)
                local model = otherPart:FindFirstAncestorOfClass("Model")

                if model then
                    local player = Players:GetPlayerFromCharacter(model)

                    if player then
                        Handle.Anchored = false
                    end
                end
            end)

        end

        AppleClone.Parent = AppleTree
    end
end
1 Like

thank you so much this helps me so much

1 Like