Hello, i made a script which takes all the AppleTrees and makes the fruit inside them transparency 1! However i want it to be random, like 1-3 apples get invisible using math.random(1, 3). How could i achieve doing that with :GetDescendants() tho? For example u interact with the prompt only 2 fruits get invisible, next time only 1(randomly however)
script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShakeTree = ReplicatedStorage:WaitForChild("InteractTree")
ShakeTree.OnClientEvent:Connect(function()
print("EVENT FIRED")
for i,v in pairs(workspace:GetDescendants()) do
if v.Name == "AppleTree" and v.ClassName == "Model" and v.Harvestable.Value == true then
for i,v in pairs(v:GetDescendants()) do
if v:IsA("BasePart") and v.Name == "ccapple" then
v.Transparency = 1
v.CanTouch = false
end
end
end
end
end)
To randomly select a number of apples to hide (between 1 and 3) for each apple tree, you can use the math.random(1, 3) function. Gather all the apples in a list, shuffle it, and then hide the chosen amount.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShakeTree = ReplicatedStorage:WaitForChild("InteractTree")
ShakeTree.OnClientEvent:Connect(function()
print("EVENT FIRED")
for _, tree in pairs(workspace:GetDescendants()) do
if tree.Name == "AppleTree" and tree.ClassName == "Model" and tree:FindFirstChild("Harvestable") and tree.Harvestable.Value == true then
local apples = {}
-- Collect all "ccapple" parts in a table
for _, part in pairs(tree:GetDescendants()) do
if part:IsA("BasePart") and part.Name == "ccapple" then
table.insert(apples, part)
end
end
-- Get a random count of apples to make invisible (1-3)
local countToMakeInvisible = math.random(1, 3)
-- Shuffle the apples and make the selected number invisible
for _, apple in pairs(apples) do
if countToMakeInvisible <= 0 then
break -- Stop once we've made enough apples invisible
end
-- Randomly choose which apples to make invisible
if math.random() < (countToMakeInvisible / #apples) then
apple.Transparency = 1
apple.CanTouch = false
countToMakeInvisible = countToMakeInvisible - 1
end
end
end
end
end)