Hello im a new scripter trying to get around and i wanted to challenge myself by making an apple tree that will spawn apples, destroy apples after they’ve been on the ground for more than a certain amount of time. and detect when a player touches it to un-anchor the apple.
So far ive made it spawn despawn but now drop, i tried to use math.random to randomly make an apple drop but that also didn’t work can anyone help?
Here’s my code. any help on this is appreciate it. please give me feed back as well if possible
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 Players = game.Players
local Lifetime = 10
local RandomApple = math.random(1, 42)
while true do
local function SpawnApple()
for i = 1, 42 do
local Apple = ApplesClone1[i]
local AppleClone = Apple:Clone()
local Handle = AppleClone:FindFirstChild("Handle")
if Handle then
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
AppleClone.Name = "Apple"
end
end
SpawnApple()
task.spawn(function()
for i = 1, 42 do
local Apple = ApplesClone1[i]
local AppleClone = Apple:Clone()
local Handle = AppleClone:FindFirstChild("Handle")
wait(math.random(1, 42))
Handle.Anchored = false
end
end)
wait(180)
local function RemoveApple()
for i, v in pairs(AppleTree:GetChildren()) do
if v.Name == "Apple" then
v:Destroy()
end
end
end
RemoveApple()
end