|
| 29 Jun 2016 07:58 PM |
I have two scripts here, one of them plays music randomly from a selection of songs. It looks like this:
wait(10)
song1 = "http://www.roblox.com/asset/?id=000000000" song2 = "http://www.roblox.com/asset/?id=111111111" song3 = "http://www.roblox.com/asset/?id=222222222"
music = Instance.new("Sound") music.Archivable = true
music.Parent = script.Parent music.Pitch = 1 music.Volume = 1 music.Looped = false music.Name = "Player"
math.randomseed(tick()) while (true) do songs = {song1,song2,song3} --There are 23 songs in total, this is just an example. song = math.random(1,#songs) var = song print("Playing Sound"..song) --script.Off.Value = false --script.Song.Value = "Song"..song music.SoundId = songs[song] music:play() wait(music.TimeLength) music:stop() end
The other one changes the color of a part depending on which song is playing:
ID = {000000000, 111111111, 222222222} --Once again, just an example
wait(10) music = game.Workspace.Speaker.Player.Player music.Changed:connect(function() if music.SoundId == "http://www.roblox.com/asset/?id="..ID[1] then script.Parent.BrickColor = BrickColor.new(327) elseif music.SoundId == "http://www.roblox.com/asset/?id="..ID[2] then script.Parent.BrickColor = BrickColor.new(1) elseif music.SoundId == "http://www.roblox.com/asset/?id="..ID[3] then script.Parent.BrickColor = BrickColor.new(37) end end)
When I go into the Solo testing mode in Studio, both scripts do as they are told - they both wait 10 seconds, and then they activate at the same time and work with each other.
As a side note, there's a bit of an issue with this - I have 18 lights that all use the color-changing script, but sometimes a few of them won't activate. I think the reasoning for this is because in the Output Window, it will sometimes say "Player is not a valid member of Part" which means it's not waiting 10 seconds, because it tries to connect to the Player (sound instance being created by the music player script) before it generates.
Anyways, for the most part, they work good in Solo. However, it's giving me that issue where they work in Studio but not in online mode. For some reason, in online mode, the music-playing script doesn't wait at all. It executes instantly as if the "wait(10)" isn't there, and on top of that, it won't stop to let the song play. I can see in the Output Window that it keeps cycling through the songs nonstop as if the "wait(music.TimeLength)" wasn't there, because there is a constant stream of "PlayingSong[#]" even though it works perfectly in Solo. Even the color-changing scripts act like this - they aren't waiting either, they're cycling through their colors as the music cycles through the songs. It's as if the wait codes don't exist at all. By the way, they ARE regular scripts in the Workspace, not LocalScripts in the StarterGUI or anything like that. Should they be Module scripts instead? I'm confused as to why the wait codes don't function correctly, but if anyone can help me out with this, I would appreciate that. |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2016 08:05 PM |
| I'm guessing I would need to do a debounce of some sort, but I'm not sure how I would apply that... |
|
|
| Report Abuse |
|
|
xKinqMill
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 545 |
|
|
| 29 Jun 2016 09:07 PM |
I have this same problem. One of my scripts work in play solo but not a test server. Why is that?
|
|
|
| Report Abuse |
|
|
|
| 29 Jun 2016 09:11 PM |
just do :WaitForChild()
#code while true do end |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2016 09:55 PM |
| The :WaitForChild helps solve the problem with the color scripts not loading, so I thank you for that. However, the problem with the music script not waiting is still there. I'm wondering if maybe I should add a "local" in there somewhere? |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2016 10:08 PM |
i dont know what randomseed is, and try parenting the sound to workspace
#code while true do end |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2016 10:09 PM |
song = song[math.random(1,#songs)]
i steal usernames, any user who talks to me is the username i stole |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2016 11:02 PM |
It's giving me the error "attempt to index global 'song' (a nil value)"
Did I do this correctly?
math.randomseed(tick()) while (true) do songs = {song1,song2,song3} song = song[math.random(1,#songs)] var = song print("Playing Sound"..song) --script.Off.Value = false --script.Song.Value = "Song"..song music.SoundId = songs[song] music:play() wait(music.TimeLength) music:stop() end |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2016 11:41 PM |
| song = songs[math.random(#songs)]; |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 08:23 AM |
"attempt to index global 'song' (a nil value)"
try putting song = song[math.random(1,#songs)]
outside of the math.randomseed
#code while true do end |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:02 AM |
When I put the line outside of the math.randomseed, it returns the error "attempt to get length of global 'songs' (a nil value)"
I think the line was fine before, it's just that the wait isn't working for some reason |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:06 AM |
| Also, it returned the error in Solo, but in online, the script didn't even function at all. |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:09 AM |
oh, put songs outside math.randomseed and put it above song, youre getting the length of songs
#code while true do end |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:22 AM |
| Believe it or not... that actually worked. It doesn't seem to wait at the beginning, but the only reason I wanted to have it wait was so that nothing would mess up while the rest of the game was loading in. It seems to function properly though, so thank you for helping. I'll report back if any other problems arise. |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:32 AM |
| Alright, so now the music-playing script doesn't repeat itself. It just plays one song and then stops. I think the songs need to be inside the math.randomseed, or else it won't repeat. Which brings us back to the original problem. Also, the fact that the wait at the beginning of the music-playing script isn't working is messing up the whole thing. When I go to play the game in online mode, the color-changing scripts for the lights doesn't work, and I think that's because the music plays too quickly. I'm really confused as to why the wait isn't functioning. |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:41 AM |
try doing
#codewhile (true) do math.randomseed(tick()) instead
#code while true do end |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:42 AM |
#code while (true) do math.randomseed(tick()) *
#code while true do end |
|
|
| Report Abuse |
|
|
Meowth552
|
  |
| Joined: 17 Dec 2009 |
| Total Posts: 2181 |
|
|
| 30 Jun 2016 09:46 AM |
| Scripts in Studio work locally on your client, while scripts on a server works serverside. The difference can mean a working script, or a broken one. |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:48 AM |
| I'm wondering if maybe I should use BindableEvents, but then again, I don't think that would solve the problem with the wait(10) not working. The scripts connect to each other just fine, but the main problem is that the wait codes are not functioning in the music-playing script, and I'm not sure how to work around that. |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:53 AM |
| Not to mention the wait(10) codes don't function in the color-changing scripts either. It seems like everything works fine, except for the wait codes, which just won't work at all for some reason. And they DO work in Studio, but not in online, as I've said plenty of times. |
|
|
| Report Abuse |
|
|
Meowth552
|
  |
| Joined: 17 Dec 2009 |
| Total Posts: 2181 |
|
|
| 30 Jun 2016 09:55 AM |
| timeLength should be written with a capital T, like so: TimeLength |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 09:56 AM |
| Sorry, I meant the :WaitForChild("Player") codes in the color scripts. Which is weird, because before they were working too, but all of a sudden they're not because I can see the "Player is not a valid member of Part" in the output window. |
|
|
| Report Abuse |
|
|
Meowth552
|
  |
| Joined: 17 Dec 2009 |
| Total Posts: 2181 |
|
|
| 30 Jun 2016 10:03 AM |
| Player isn't a valid member of Part. Could you post the script with the :WaitForChild in it? |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 10:07 AM |
Go to online mode and type in chat "/console" This will open up a output box. You might be able to find something out that way. |
|
|
| Report Abuse |
|
|
|
| 30 Jun 2016 10:24 AM |
@Meowth552
ID = {000000000, 111111111, 222222222}
music = game.Workspace.Speaker.Player:WaitForChild("Player") music.Changed:connect(function() if music.SoundId == "http://www.roblox.com/asset/?id="..ID[1] then script.Parent.BrickColor = BrickColor.new(327) elseif music.SoundId == "http://www.roblox.com/asset/?id="..ID[2] then script.Parent.BrickColor = BrickColor.new(1) elseif music.SoundId == "http://www.roblox.com/asset/?id="..ID[3] then script.Parent.BrickColor = BrickColor.new(37) end end)
@dudefun1234
I checked that, it just says the same stream of "PlayingSound[#]" as it does in the Studio output window. |
|
|
| Report Abuse |
|
|