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

Why won't my script work?

I'm trying to make this script play random music each time, But It Won't Work any Suggestions or ideas? What did I do wrong?

local X = math.random(5)
local BaseUrl = "http://www.roblox.com/asset/?id="
if X == 1 then
	script.Parent.menu_sfx_audio.SoundId = BaseUrl.."199768665"
	wait(.5)
	script.Parent.menu_sfx_audio.Play()
else if X == 2 then
	script.Parent.menu_sfx_audio.SoundId = BaseUrl.."199768665"
	wait(.5)
	script.Parent.menu_sfx_audio.Play()
else if X == 3 then
	script.Parent.menu_sfx_audio.SoundId = BaseUrl.."199768665"
	wait(.5)
	script.Parent.menu_sfx_audio.Play()
else if X == 4 then
	script.Parent.menu_sfx_audio.SoundId = BaseUrl.."167135038"
	wait(.5)
	script.Parent.menu_sfx_audio.Play()
else if X == 5 then
	script.Parent.menu_sfx_audio.SoundId = BaseUrl.."185601181"
	wait(.5)
	script.Parent.menu_sfx_audio.Play()
end

3 comments

0

Incorrect Syntax's: Lines 7-22; It should be 'elseif', and to call a method, you should use a Colon ':' (E.x. 'Sound:Play()').

1

Just change . for : like I do -.-

1

And change else if for elseif

2 answers

Your problem seems to be you're saying else if as in two different words. You're probably reading it like this;

else if then ... --The code should work.

while the script is reading it like this;

else
	if then ...
	--No end, we'll error out!

You seem to want the elseif keyword, which means, if the first conditions isn't right, then we need to go to else, oh wait, there's a if with that else so we need to check if the condition is correct.

So in short terms, remove the space between the else and if


Also, you need to change the .Play() to :Play() since it is a method. Methods are always called like this using a :NameOfMethod(). So you will also want to change the . before Play to :.


local X = math.random(5)
local BaseUrl = "http://www.roblox.com/asset/?id="
if X == 1 then
    script.Parent.menu_sfx_audio.SoundId = BaseUrl.."199768665"
    wait(.5)
    script.Parent.menu_sfx_audio:Play()
elseif X == 2 then
    script.Parent.menu_sfx_audio.SoundId = BaseUrl.."199768665"
    wait(.5)
    script.Parent.menu_sfx_audio:Play()
elseif X == 3 then
    script.Parent.menu_sfx_audio.SoundId = BaseUrl.."199768665"
    wait(.5)
    script.Parent.menu_sfx_audio:Play()
elseif X == 4 then
    script.Parent.menu_sfx_audio.SoundId = BaseUrl.."167135038"
    wait(.5)
    script.Parent.menu_sfx_audio:Play()
elseif X == 5 then
    script.Parent.menu_sfx_audio.SoundId = BaseUrl.."185601181"
    wait(.5)
    script.Parent.menu_sfx_audio:Play()
end

Still errors out? In game, press F9 and tell us the error coming from the script, or in studio, look at the output and tell us the error!

I am not so sure but I don't think the top locals need to be there

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