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

Why won't my script work?

Asked by 9 years ago

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
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()'). TheeDeathCaster 2368 — 9y
1
Just change . for : like I do -.- XToonLinkX123 580 — 9y
1
And change else if for elseif XToonLinkX123 580 — 9y

2 answers

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

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!

Ad
Log in to vote
-3
Answered by 9 years ago

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

Answer this question