Hello everyone, this is a tutorial about math.random in scripts that can be used in many ways. I will show you how you can use math.random with positions. If you think you’re missing something, respond to this topic and others may help you and I might aswell change this topic if people request it.
- Info before we start:
math.random is always a number, if its not it will call out an error. An example of a good script with math.random:
local robux = 1 -- variables
local tix = 16
local buildersclub = 500
local egg = 80
local variables = {robux, tix, buildersclub, egg}
local chosenVariable = variables[math.random(1, #variables)] -- This will pick one of the 4 items in the table
print(chosenVariable) -- This will print out one of the items we have putten in the variable table
link to roblox resources:
math | Roblox Creator Documentation
Random | Roblox Creator Documentation
-
Add a script to Workspace/ServiceScriptService and name it whatever you would like.
-
the script:
local raindrops = 0 -- amount of parts dropped, I will come back about this later in the script
local function rainFunc() -- making a function called rainFunc that we can use late in the script
local x = math.random(-20,40) -- You can’t say (40, -20) since 40 is not greater than -20.
local y = 20 -- If you want the Y-axis to be higher, do something like: math.random(20,30), which will pick a number between 20 and 30
local z = math.random(-20,40)
local raindrop = Instance.new('Part') -- Inserting a part
raindrop.Name = 'raindrop' -- Naming the part
raindrop.BrickColor = BrickColor.new('Pastel Blue') -- These are all the properties of the part
raindrop.Size = Vector3.new(0.5,1,0.5)
raindrop.Parent = workspace
raindrop.Position = Vector3.new(x,y,z)
raindrop.Anchored = false
raindrops = raindrops + 1 -- As we saw earlier in the script: Local raindrops = 0, so every time this function is called, that variable will update
end
while task.wait() do -- This is the script that keeps looping around
if raindrops <= 250 then -- Basically, if local raindrops = less than 250, then we call the function
rainFunc()
elseif raindrops >= 250 then -- If local raindrops = more than 250, then we go further to the pairs which is an iterater function
for i,v in pairs(game.Workspace:GetChildren()) do -- game.Workspace:GetChildren is basically just getting the children from the Workspace which we can use later on in the game
wait(0.1) -- it will loop through this in pairs loop every 0.1 second
if v.Name == 'raindrop' then -- We are now going through all the instances in the workspace and look for a v(value) called 'raindrop'
v:Destroy() -- If the name of the value is 'raindrop' then we destroy that instance
end
end
end
end
- End product:
Check the script yourself and when you’re finished let me know what you think about this tutorial. Now, for the more advanced ones reading this script. The script is supposed to be as simplified as possible.