How to move parts from one script

So, basically I want to move the apples in a certain position when they’re moving up and down. I tried using TweenService for the apples to move up and down, but it won’t let the apples move to a random position. I also tried to loop through all the folder’s children and move the apple up and down, but it didn’t work.

How this basically works is, when a player touches an apple, the apple will move to a random position while moving up and down.

I have the apple model in ServerStorage, it will be cloned to the workspace later. Its parent is a folder, so right now there are multiple scripts. Each script is parented to one apple model, which is what I do not want. Since it’s unnecessary having multiple scripts. I wonder if there is another way to this, also a way that doesn’t reduce the performance.

Screenshot 2023-10-08 081922

Screenshot 2023-10-08 082807

This is my apple script that makes it move up and down without TweenService (So, it is not smooth).

while true do
	for i = 1, 10, 1 do
		script.Parent.PrimaryPart.CFrame *= CFrame.new(0, 0.1, 0)
		task.wait(0.1)
	end
	
	for i = 1, 10, 1 do
		script.Parent.PrimaryPart.CFrame *= CFrame.new(0, -0.1, 0)
		task.wait(0.1)
	end
end

You should Tween it to a math.random value for the x and z components.

-- Assuming you create the apples procedurally

local TweenService = game:GetService("TweenService")

local T_MAX_R = 50

workspace.Food.ChildAdded:Connect(function(food)
    if not food:IsA("BasePart") or not food:IsA("Model") then return end
    -- add other conditions.

    local prim = food.PrimaryPart if food:IsA("Model") else food

    prim.Touched:Connect(function(hit)
         -- Reject touch detection from debris
         if not hit or not hit.Parent then return end

         local player = game.Players: GetPlayerFromCharacter(hit.Parent)

         -- Do not handle touch from other non-player objects
         if not player then return end

         local tw = TweenService:Create(prim, TweenInfo.new(), {CFrame = prim.CFrame + Vector3.new(math.random(-T_MAX_R, T_MAX_R), 2, math.random(-T_MAX_R, T_MAX_R))})

         tw:Play()
         tw.Completed:Once(function()
              food:Destroy()
         end)
     end)
end)

Ok, so this is how I wanted to be. The food once touched is moved to a random position while moving up and down. So, it’s never destroyed.