Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script only showing a block of wheat?

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.

2 comments

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.

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.

2 answers

✓ Accepted answer

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.

2 comments

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?

0

Huh? DM me FadedJayden#7171

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!

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.