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

Why is math.random not working with "v" in this script? "Bad argument given

So, basically, I made this script, where when you step on this part, a random body part of yours catches fire. Here's the 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()
			
			for i,v in pairs(myArray) do
				
				fire:Clone().Parent = math.random(v)
				wait(2)
				
				db = false
			        end
			    end
			end
		end
	
	
part.Touched:connect(burn)

It's supposed to take one of the bodyparts in "myArray", but it doesn't and says I gave it a bad argument. Any ideas? All help is appreciated!

2 answers

✓ Accepted answer

Your first problem is that you used math.random() improperly. It requires a start and end point from which it can select a random value. What you did was not feed it a number, but feed it an object, which it won't be able to understand. The generic for is also completely unnecessary, when rather than running through all of the values, you could've just ran math.random() once and indexed the value it returned.

Your code:

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()

            for i,v in pairs(myArray) do

                fire:Clone().Parent = math.random(v)
                wait(2)

                db = false
                    end
                end
            end
        end


part.Touched:connect(burn)

Your code with changes:

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)

                db = false
                end
            end
        end


part.Touched:connect(burn)

If this helped you, please don't forget to upvote and accept the answer :)

You don't need to loop through the table. Just place the fire on a random object in the table. To get something randomly out of a table, you would index a random value within the index of the table.

local part = script.Parent
local db = false

function burn(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")

	if human then
		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 myArray = {p1, p2, p3, p4, p5}

		fire.Parent = myArray[math.random(#myArray)]
	end
end


part.Touched:Connect(burn)

That's the answer for how I understood your question. If this isn't what you wanted, please tell me exactly what you want in the comments below.

Good Luck!

If I helped, please don't forget to accept my answer.

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