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

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

Asked by
Peeshavee 226 Moderation Voter
8 years ago

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:

01local part = script.Parent
02local db = false
03 
04function burn(hit)
05 
06    local p1 = hit.Parent:FindFirstChild("Left Arm")
07    local p2 = hit.Parent:FindFirstChild("Right Arm")
08    local p3 = hit.Parent:FindFirstChild("Left Leg")
09    local p4 = hit.Parent:FindFirstChild("Right Leg")
10    local p5 = hit.Parent:FindFirstChild("Torso")
11    local fire = Instance.new("Fire")
12    local human = hit.Parent:FindFirstChild("Humanoid")
13    local myArray = {p1, p2, p3, p4, p5}
14 
15    if human then
View all 34 lines...

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

Log in to vote
0
Answered by 8 years ago

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:

01local part = script.Parent
02local db = false
03 
04function burn(hit)
05 
06    local p1 = hit.Parent:FindFirstChild("Left Arm")
07    local p2 = hit.Parent:FindFirstChild("Right Arm")
08    local p3 = hit.Parent:FindFirstChild("Left Leg")
09    local p4 = hit.Parent:FindFirstChild("Right Leg")
10    local p5 = hit.Parent:FindFirstChild("Torso")
11    local fire = Instance.new("Fire")
12    local human = hit.Parent:FindFirstChild("Humanoid")
13    local myArray = {p1, p2, p3, p4, p5}
14 
15    if human then
View all 34 lines...

Your code with changes:

01local part = script.Parent
02local db = false
03 
04function burn(hit)
05 
06    local p1 = hit.Parent:FindFirstChild("Left Arm")
07    local p2 = hit.Parent:FindFirstChild("Right Arm")
08    local p3 = hit.Parent:FindFirstChild("Left Leg")
09    local p4 = hit.Parent:FindFirstChild("Right Leg")
10    local p5 = hit.Parent:FindFirstChild("Torso")
11    local fire = Instance.new("Fire")
12    local human = hit.Parent:FindFirstChild("Humanoid")
13    local myArray = {p1, p2, p3, p4, p5}
14 
15    if human then
View all 32 lines...

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

Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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.

01local part = script.Parent
02local db = false
03 
04function burn(hit)
05    local human = hit.Parent:FindFirstChild("Humanoid")
06 
07    if human then
08        local p1 = hit.Parent:FindFirstChild("Left Arm")
09        local p2 = hit.Parent:FindFirstChild("Right Arm")
10        local p3 = hit.Parent:FindFirstChild("Left Leg")
11        local p4 = hit.Parent:FindFirstChild("Right Leg")
12        local p5 = hit.Parent:FindFirstChild("Torso")
13        local fire = Instance.new("Fire")
14        local myArray = {p1, p2, p3, p4, p5}
15 
View all 21 lines...

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.

Answer this question