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

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

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

1 comment

0

Please use code blocks

2 answers

✓ Accepted answer

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


1 comment

0

It worked.Thank you very much for helping me understand the problem and be able to use math.random in the future.

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

1 comment

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.

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