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

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

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

5 comments

0

its 'game.Workspace' not 'game.workspace' because Lua is cAsE sEnSeTiVe

0

And instead of typing game.Workspace.Runner make use of the variable you created called Runner. Plus a typo on Line 11

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.

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.

0

How is workspace deprecated? For me, when i type workspace, nothing happens, but when I type Workspace, something happens

1 answer

✓ Accepted answer

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.

3 comments

0

Also, if Runner is a model, you're not able to use .Position, I think.

0

@NathanTheDeveloper is right if it is a model then you would have to use :MoveTo()

0

@NathanTheDeveloper why didn't you just write local Choose = math.random(1, 3) ? It would get rid of three lines of code.

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