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

What is wrong with this code?. Not sure what the problem is with this script.Help appreciated

Hello, I stopped attempting to learn roblox lua about three months ago, how ever I am getting back into it, and have been going over the basics. But, I don't understand what is wrong with the script. I am just trying to practice right now but I am having trouble, help + what I did wrong would be greatly appreciated!! Thanks.

thevariable= 5
thewrongnumber= 7
wait (5) 
if thewrongnumber == 6 then
	hello()
else if
	thevariable == 5
then	hello()
	end


function hello()
	game.Workspace.Test.BrickColor = BrickColor.new("Black")
	wait(5)
	game.Workspace.Test.BrickColor = BrickColor.new("Purple")
	return
end

3 comments

0

1.) Get rid of the odd spacing, and correctly indent your code (elseif no space and wait() no space. Also, variable declarations look better if there is a space between the variable and the = sign example: local x = true). 2.) Define your variables with local (example: local thevariable). 3.) Define your function before calling it and define it with local as well(example: local function hello()).

0

4.) Don't use return if it serves no purpose. You are not returning a value and the function ends by default at that point anyway. 5.) Use variables effectively for efficiency and ease. You say game.Workspace.Test twice. That is bad enough. Imagine if you had to type it more times. So, create a varaible for test.

0

Thanks, I will take your advice :D

1 answer

✓ Accepted answer

you cant call a function, if it wasnt made yet. so you have to change some stuff


local thevariable= 5
local thewrongnumber= 7

local function hello()
	game.Workspace.Test.BrickColor = BrickColor.new("Black")
	wait(5)
	game.Workspace.Test.BrickColor = BrickColor.new("Purple")
  
end

	
if thewrongnumber == 6 then
	hello()
else if thevariable == 5 then
	hello()
end
	 

sooo there were a lot of other problems and the one that u hav to fix is spacing, your script gotta be clean, im not sure if its something with your ctrl+c and ctrl+v but if youre working like that in a real script, you gotta fix this my dude. AND NEVER, I REPEAT, NEVER DO A WAIT AT THE TOP OF A SCRIPT.

5 comments

1

Placing a wait at the top of a script can be beneficial in many circumstances. That part of your answer is incorrect. There is nothing wrong with putting a wait at the top of a script.

0

ahem, alvinblox reference

0

So? Don't discourage a beginner with poor logic.

0

Wrong. You can call a function which wasn't declared yet, as long as it is a global funcrion, which is the case in OP's script.

0

oh you cant i didnt know

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