Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script only showing a block of wheat?

Asked by
Time_URSS 146
5 years ago

Greetings,

I and my friend made a wheat farm, and we wanted to make a script that makes it select a random number of wheat and make it disappear (like a 30% shows and the rest is gone). How would I start doing it? Maybe using math? Or other parameters? Any answer is helpful.

0
Select a random number from 1 to (how much wheat you have): x = math.random(1, #wheat). The amount of wheat you lose is that number subtracted by the total amount of wheat. y = #wheat - x. pidgey 548 — 5y
0
To get rid of random wheat, make a loop that runs y times to index a random position of a table with all the wheat (if they're all grouped, GetChildren will do) and use the Destroy function in the object of that position of the table. pidgey 548 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

I tested it out and it works used ClickDetector to fire my script.

script.Parent.ClickDetector.MouseClick:Connect(function()
    local child = game.workspace.Folder:GetChildren()
    local x = 30/100*#child
    local y = math.ceil(x)
    print(y)
    for i = 1,y do
        child[i]:Destroy()
    end
end)

So I rounded down the value of x which is 30% of the entire wheat. I parented the wheat to a Folder which then I used :GetChildren() to retrieve all the wheat. I then used the operator '#' to get the count of the amount of wheat. So 30/100*#child. Which then I rounded it up to prevent decimals.

0
Now if I wanted to make a script that manes if you click a wheat block, it generates a random one from the list? Time_URSS 146 — 5y
0
Huh? DM me FadedJayden#7171 FadedJayden_Dev 118 — 5y
Ad
Log in to vote
0
Answered by
Arkrei 389 Moderation Voter
5 years ago

A bit of math.random magic would do the trick

local ModelThing = workspace.Model:GetChildren
for i = 0,math.random(1,#ModelThing),1 do -- loop for a random amount of times depending on how many children are in the model
    ModelThing[i].Transparency = 1 -- index the table from getchildren and change the transparency to 1
end

I'm not 100% sure this will work so let me know if you have issues with it!

0
GetChildren needs parenthesis because it's a function, also, the step for the for loop is redundant because it goes up by 1 by default. Also, that will remove the objects in order of the table, I think that OP wants it to be random. Lua tables do not have a 0th index, so that will be a run-time error as well. pidgey 548 — 5y

Answer this question