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

How do I use the part that was chosen in math.random?

So, so far I have my script made in a way, that when I step on the part it randomly chooses a part in "myArray". Now, I want it that when you "wait(2)", the part that was chosen is destroyed Here's my script:

local part = script.Parent
local db = false

function burn(hit)
	
	local p1 = hit.Parent:FindFirstChild("Left Arm")
	local p2 = hit.Parent:FindFirstChild("Right Arm")
	local p3 = hit.Parent:FindFirstChild("Left Leg")
	local p4 = hit.Parent:FindFirstChild("Right Leg")
	local p5 = hit.Parent:FindFirstChild("Torso")
	local fire = Instance.new("Fire")
	local human = hit.Parent:FindFirstChild("Humanoid")
	local myArray = {p1, p2, p3, p4, p5}
	
	if human then
		if not db then
			
			db = true
			
			part.Sound:Play()
			
			
				
				fire:Clone().Parent = myArray [math.random(1,5)]
				wait(2)
                                --chosenpart:Destroy()
				
				
				db = false
			        
			    end
			end
		end
	
	
part.Touched:connect(burn)

All help is appreciated!

5 comments

0

this script chooses multiple parts.

0

It takes one of the parts "math.random" chose. Now, I just want to know how to call the part that was chosen. I just don't know what to call it.

1

part = myArray[math.random(#myArray)]

1

Thanks! Works great. So basically, I need to create a variable using math.random BEFORE I tell it what it's parent is?

1

Yea, sure.

1 answer

✓ Accepted answer

It is easy! You just need to make a variable for the chosen brick! Also, using # returns the number of items in an array. Okay! Let's fix your script!

local part = script.Parent
local db = false

function burn(hit)

    local p1 = hit.Parent:FindFirstChild("Left Arm")
    local p2 = hit.Parent:FindFirstChild("Right Arm")
    local p3 = hit.Parent:FindFirstChild("Left Leg")
    local p4 = hit.Parent:FindFirstChild("Right Leg")
    local p5 = hit.Parent:FindFirstChild("Torso")
    local human = hit.Parent:FindFirstChild("Humanoid")
    local myArray = {p1, p2, p3, p4, p5}

    if human then
        if not db then

            db = true

            part.Sound:Play()

		local chosenpart = myArray [math.random(1, #myArray)] --Returns the number of bricks that are defined in myArray!
		local fire = Instance.new("Fire", chosenpart) --I'd rather doing it this way!  Pass the Parent as the second argument, but this is optional!
                wait(2)
                chosenpart:Destroy()


                db = false

                end
            end
        end


part.Touched:connect(burn)

If you have any questions, please leave a comment below. Thank you and I hope this will help you! :)

-CStarLam

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