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
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
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