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

Trying to make a randomizer for this part (inside workspace). it isn't working 0_0. Any ideas?

Asked by 7 years ago
Runner = game.workspace.Runner
Choose = math.random(1,3)
A=1
B=2


if Choose == 1 then
game.workspace.Runner.Position = Vector3.new(20,20,20)
end
if Choose == 2 then
game.worksapce.Runner.Position = Vector3.new(30,30,30)
end
if Choose == 3 then
game.workspace.Runner.Postion = Vector3.new(40,40,40)
end
0
its 'game.Workspace' not 'game.workspace' because Lua is cAsE sEnSeTiVe PlayingOBC 70 — 7y
0
And instead of typing game.Workspace.Runner make use of the variable you created called Runner. Plus a typo on Line 11 PlayingOBC 70 — 7y
1
You could use elseifs instead of multiple if statements. Also, you could just make a variable that store, say "Choose*10+10" and use that variable when changing position. You wouldn't need if statements at all then. GoldenPhysics 474 — 7y
0
@PlayingOBC For that, it doesn't matter. It's neater to put game.Workspace, but it'll still know what you're wanting to do. Async_io 908 — 7y
0
How is workspace deprecated? For me, when i type workspace, nothing happens, but when I type Workspace, something happens brickzay 0 — 7y

1 answer

Log in to vote
1
Answered by
Async_io 908 Moderation Voter
7 years ago
Edited 7 years ago

Please read all of the answer, as I put a lot of time and effort into it and it would be highly appreciated

First thing's first

Indenting,

Indenting is an important part of coding that allows other scripters to see what it is you're trying to code. Without indenting, it makes it extremely hard to read. Please indent your code by using the TAB key while in Roblox Studio so that we may better read your code. If you don't know how to properly indent, I will provide you with an example.*

local var = "Help me please...."

function printMsg(msg)
    if msg == ("Help me please....") then --Indented because it's in a function
        print(msg) --Indented because it's the inside of if
    else --Indented back because it's part of the if statement, so it lines up with if
        print("The message isn't what it says it is =(") --Indented because it's the inside of if
    end --Indented to line up with the if
end --Indented to line up with the function

printMsg(var) --Does not need indenting because it's not in anything

An easier way to think of indenting is like a forest. Each function is it's own tree, and everything inside of the function is it's own branch. When you're connecting a smaller branch to a big branch, you'll put the code inside the code. So for instance, an if statement is a bigger branch, you'll attach some little branches of code to that so it knows what it's supposed to do, then you'll have a bigger branch next to that called else, and you'll make little branches of code for the else.

Organizing and cleaning your code,

It's helpful to have a cleaned, organized code, not only for you to debug but for others. To do so, it's better to have locals for variables and keep your variables at the beginning of the script unless it is not yet defined. A lot of the organization-based stuff is on you, but indenting and organizing definitely is a must.

FINISHED SCRIPT

local runner = game.workspace:WaitForChild('Runner') --Waits for the runner.

function randomNumber(min, max) --math.Random will not update if not in a called function
    math.random(min, max)
end

local Choose = randomNumber(1, 3)
if Choose == 1 then
    game.workspace.Runner.Position = Vector3.new(20,20,20)
elseif Choose == 2 then
    game.workspace.Runner.Position = Vector3.new(30,30,30)
elseif Choose == 3 then
    game.workspace.Runner.Postion = Vector3.new(40,40,40)
end

The scripting output is also where you're going to find your errors. To enable that, simply go to view tab, and click output.

0
Also, if Runner is a model, you're not able to use .Position, I think. Async_io 908 — 7y
0
@NathanTheDeveloper is right if it is a model then you would have to use :MoveTo() shadownetwork 233 — 7y
0
@NathanTheDeveloper why didn't you just write local Choose = math.random(1, 3) ? It would get rid of three lines of code. shadownetwork 233 — 7y
Ad

Answer this question