This script randomly picks a song and is supposed to play it, but i get an error saying "Line 5: attempt to index global 'songplay' (a boolean value)" Any way to fix this? Also, i have a script that will enable this after a certain time.
local songs = game.Workspace.IntermissionMusic local song = songs:GetChildren() local picked = song[math.random(1, #song)] songplay = picked:clone().Parent == game.Workspace songplay:play() wait (125) songplay:stop()
Thanks!
You're trying to do too many things all at once on line 4.
What you've asked your script to do is this: "Make a clone of 'picked' and then tell me if the Parent of that clone is the workspace and assign that boolean value to 'songplay'." What you want is this:
songplay = picked:clone() --assign the clone to 'songplay' songplay.Parent = workspace --assign the parent of the clone to the workspace songplay:play() --continue on with the rest of your script as normal
Consider looking through the Sound API. It has events like Ended
and properties like TimeLength
, which you might want to make use of.
Your problem is line 4: the way it's set up, it's saying "Clone's Parent == Workspace," not "songplay is a song clone, now after set the parent." You're essentially doing on line 5 BOOL:Play()
, when that's not possible.
The solution would be to separate the two: take out the .Parent == game.Workspace,
b/c it's not setting the parent, and ==
means to compare two values.
local NewSong = RandomlyPickedSong:Clone() -- The Parent comparison to the Workspace is on the next line now, and is no longer a comparison. NewSong.Parent = game.Workspace -- Sets the parent, instead of comparing the two & giving a value. NewSong:Play() -- Uppercase the first letter in "Play," "Stop," and "Pause," as it's much better to use.
Now, as to how I concluded this, if you'd've printed line 4, it would've looked something like this:
print(songplay) --> false
Believe it or not, this was very simple. XP Dw though, sometimes I miss stuff like this too. :)
Now to recap on ==
, it means to compare (two) values, like if value2 == value2 then
; the ==
operator is commonly shown to be used in if statements, b/c it's an operator used to compare (two) values; try to remember this if you can't remember: =
means to set a value, while two of them together means to compare values. :)
Here's an example of the ==
operator, and how your script interpreted it:
local String = 'I\'m in a string! :D' local CompareOperatorExample = String == 'I\'m in a string! :D' print(CompareOperatorExample) --> true CompareOperatorExample = String == 'I\'m alive! :D' print(CompareOperatorExample) --> false -- This can also be done w/ a if statement as well: local String = 'I\'m in a string! :D' local CompareOperatorExample = String == 'I\'M MAGIC!' if CompareOperatorExample then print('MERASMUS') --> [Prints nothing] else print('NO MAGIC 4 YOU') --> NO MAGIC 4 YOU end -- You can also check if a value doesn't compare to another, by using the "~=" operator; "~=" means "value1 doesn't compare/ equal to value2," or to put it simpler, the opposite of "==": local String = 'I am alive :)' local CompareOperatorExample = String ~= 'I am alive :)' print(CompareOperatorExample) --> false [in contrast, if you had used "==," this would've returned true]
Hope this helped you in any way! :D