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

Where do I place this math.random script?

Asked by 7 years ago
Edited 7 years ago

Okay, I have come up with a script thats supposed to change the color of my Baseplate in Workspace. Where should I put the script? In ServerScriptStorage or Workspace. Either way, it still doesn't work. Please help.

number = math.random(1,5)

if number == 4 then
    game.Workspace.Baseplate.BrickColor = BrickColor.Red()
end
if number == 2 then
    game.Workspace.Baseplate.BrickColor = BrickColor.Green()
end
if number == 3 then
    game.Workspace.Baseplate.BrickColor = BrickColor.Yellow()
end
if number == 1 then
    game.Workspace.Baseplate.BrickColor = BrickColor.Orange()
end
if number == 5 then
    game.Workspace.Baseplate.BrickColor = BrickColor.Orange()

end


1 answer

Log in to vote
0
Answered by 7 years ago

Notes:

  • Make sure this is a Script, not a LocalScript (see this guide if you want to know why)
  • Since it's a Script, you can put it in either of the locations you mention since you aren't using script.Parent. It is better to put it in ServerScriptService since the existence of this script does not need to be replicated to clients.
  • You can simplify your script by using a table:
local colors = {BrickColor.Red(), BrickColor.Green(), BrickColor.Yellow(), BrickColor.Orange(), BrickColor.Orange()}
local baseplate = workspace.Baseplate
function ChangeColor()
    baseplate.BrickColor = colors[math.random(1, #colors)]
end
ChangeColor()
  • Your script only assigns one colour, then it's done. If you want it to do this more regularly, you will need to either use a loop with a wait command, or connect it all to an event.
--Changing the color every second:
while true do
    wait(1)
    ChangeColor()
end
--Changing the color every time someone enters the place
game.Players.PlayerAdded:connect(ChangeColor)
Ad

Answer this question