'Can I make math.random set brick colors?
I'm playing roblox and I want to develop a script where when Del is pressed, it will choose a random number from 1-3. I have 3 parts, one named one, one named two, and one named three. Whichever number math.random chooses, it sets the corresponding brick of that number to Lime Green. This one doesn't work because it's going in a pattern of 2,3,1 and I want it random. I've tried 8 times and it's gone in the exactly same pattern every time. Anyways, here's my script:
local choices = { game.Workspace.one.Part, game.Workspace.two.Part, game.Workspace.three.Part }
local space = game.Workspace
local P1 = space.one
local P2 = space.two
local P3 = space.three
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Delete then
local index = math.random(#choices)
choices[index].BrickColor = BrickColor.new(0, 255, 0)
wait(15)
choices[index].BrickColor = BrickColor.Black()
end
end)
Solution 1:[1]
In Lua < 5.4 you need to set a random seed yourself. Otherwise math.random will create the same sequence of random numbers every time you execute your script.
A common way to get a new seed every time is to use the system time as a seed value.
Call math.randomseed(os.time()) at the beginning of your script.
See https://developer.roblox.com/en-us/api-reference/lua-docs/math
This was actually part of the answer to your last question. (Roblox's Lua is derived from Lua 5.1)
Please read answers carefully.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Piglet |
