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

My script is giving me an error that i dont know how to fix on math.random?

Asked by 4 years ago

I got an error and it seems like my way of randomising the item location is wrong.Can anybody tell me what is wrong with it?It will be a really good help.The error says:"Workspace.Part.Script:6:wrong number or arguments."The part is in Workspace,the part being with a script inside it.Also i ran the test in run mode in Roblox Studio and the print doesnt seem to work either,if you dont know what line to look at you should look at line 6.Sorry that the script looks bad in the website.I'm new to it.

local brick = script.Parent

brick.CanCollide = false brick.Parent = game.Workspace

local key = math.random(1,2,3,4) if key == 1 then brick.Position = Vector3.new(-116.75,18.859,387.5) print("first") elseif key == 2 then brick.Position = Vector3.new(-156.75,18.859,389.5) print("second") elseif key == 3 then brick.Position = Vector3.new(-135.25,18.859,368.5) print("third") elseif key == 4 then brick.Position = Vector3.new(-135.75, 18.859, 387.5) print("fourth") end

0
Please use code blocks Leamir 3138 — 4y

2 answers

Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago

Hello, SitaruDaniel!


local brick = script.Parent brick.CanCollide = false brick.Parent = game.Workspace local key = math.random(1,4) -- Arguments of math.random: min, max if key == 1 then brick.Position = Vector3.new(-116.75,18.859,387.5) print("first") elseif key == 2 then brick.Position = Vector3.new(-156.75,18.859,389.5) print("second") elseif key == 3 then brick.Position = Vector3.new(-135.25,18.859,368.5) print("third") elseif key == 4 then brick.Position = Vector3.new(-135.75, 18.859, 387.5) print("fourth") end
0
It worked.Thank you very much for helping me understand the problem and be able to use math.random in the future. SitaruDaniel 44 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Hi Sitaru, There is an issue with your script, and also an enhancement that can be made to your script. So, let's get right into it!

Issue

First off, the issue is the arguments for math.random(). Math.random() takes two arguments, one being the minimum value that can be produced by the random generator and one is the maximum.

Go here to find out more about math.random()

Enhancement

I don't recommend you use so many if statements for different conditions. Rather, you should use a table of vectors and just access that table for each condition. That way you don't have to write as much (Us programmers tend to be lazy.)

Here is how you could go about that:

local vectors = {  -- Contains all of your vectors
    Vector3.new(-116.75,18.859,387.5),
    Vector3.new(-156.75,18.859,389.5),
    Vector3.new(-135.25,18.859,368.5),
    Vector3.new(-135.75, 18.859, 387.5),
}

local key = math.random(1, 4)
brick.Position = vectors[key] -- Accesses the vector in the table, using the key as an index.

Go here to find out more about tables.

Thanks,

Best regards,

IdealstDeveloper

0
Didn't know i can use tables for this.Im new to tables so thank you for telling me how to make it more efficient. SitaruDaniel 44 — 4y

Answer this question