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

Remote event not firing in a while true do loop when more than 1 player joins?

Asked by 5 years ago
Edited 5 years ago

The script works fine for when the first player joins and starts the server (playing the music and showing the song name) but when another player joins, it just plays the music and doesn't show the song name for them until it switches to another song.

Script in Sound:

01local Sound = Instance.new("Sound", game.Workspace)
02 
03Sound.Name = "SongAcreol2"
04 
05local musicList = require(game.ServerScriptService.SongList)
06 
07local Event = game.ReplicatedStorage.ApoxyzMusicChange
08 
09local musicName = musicList
10 
11local Activate = false
12 
13local keys = {}
14 
15for key, _ in pairs(musicList) do
View all 86 lines...

Local Script in TextLabel:

1local Event = game.ReplicatedStorage.ApoxyzMusicChange
2 
3Event.OnClientEvent:Connect(function(musicName)
4    if musicName then
5        script.Parent.Text = musicName
6    end
7end)

2 answers

Log in to vote
0
Answered by
Torren_Mr 334 Moderation Voter
5 years ago

You forgot to make the Activate value false at the end.

Try using this code:

01local Event = game.ReplicatedStorage.ApoxyzMusicChange
02 
03local Activate = false
04 
05game.Players.PlayerAdded:Connect(function()
06 
07    if Activate == false then
08 
09        Activate = true
10 
11        while true do
12 
13            wait(0.5)
14 
15            local randomKey = keys[math.random(#keys)]
View all 29 lines...

If the script above doesn't work in the way you wanted it to, try moving the line I added 2 lines below.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Hey bud, I was helping you out with your other post on this. All you need to do to fix this is add one line of code and change the order around a little. Here is your updated code:

01local Event = game.ReplicatedStorage.ApoxyzMusicChange
02local musicName = "" -- moved the scope outside so it can be used outside of the while loop
03local Activate = false
04 
05game.Players.PlayerAdded:Connect(function(player) --Added player to capture the newly joined player
06 
07    if Activate == false then
08 
09        Activate = true
10 
11        while true do
12 
13            wait(0.5)
14 
15            local randomKey = keys[math.random(#keys)]
View all 29 lines...

Also, I remember your other post on this, so don't change Activate back to false. You wanted the while loop to run only once as I remember. I hope this helps out! If it doesn't, please keep this post opened and add a comment under my post. I check my answers for feedback regularly.

I also noticed that you structured your code differently than what I did to prevent the Studio bug. Unless they fixed this (it's been around for a long time, so doubtful) you will definitely experience the bug as your game grows. It's ultimately your choice, but I promise it will save you some headache down the line.

[EDIT] The second problem probably should've been in another post since it was new problems based off of your changes, but I fixed it for you anyway. All I did was move some things around a little bit, you were definitely on the right track.

Here is the updated script:

01local Sound = Instance.new("Sound", game.Workspace)
02Sound.Name = "SongAcreol2"
03local musicList = { --replace this with your module script
04    ["356718047"] = "N'to Trauma",
05    ["372492410"] = "Tell Me Why",
06    ["2468961271"] = "Mine The Diamond"
07    }
08local Event = game.ReplicatedStorage.ApoxyzMusicChange
09local musicName --If you have a huge song list, then it's better not to have the same information in two places. No need to set this to any value.
10local Activate = false
11local keys = {}
12local Change = game.ReplicatedStorage.ChangeSong
13local Skip = game.ReplicatedStorage.SkipSong
14 
15for key, _ in pairs(musicList) do
View all 59 lines...

A good rule of thumb is don't embed event-driven functions within one another. If you need several functions to use the same variables, then increase the scope of the variables to the highest scope. Be careful when doing this though, you'll need to account for the different values of the variable in the different functions.

I tested it as well in Studio for single and multiple players with the skip button, without the skip button, letting the song finish, and combinations of the three.

0
Seems to be working great, thank you, this has been aggravating me for awhile. I deleted the other post because I didn't realize you'd come back to check the comment I posted. This code is supposed to run constantly, not just once but it still works and the song changes and shows the name for everyone even when they join. Thank you again! Asynchronize 16 — 5y
0
Any time, glad to help! SteelMettle1 394 — 5y
0
Yeah it broke again, it works great when you first join but when the song switches it just breaks. I'm getting sick of this, do you have any suggestions? I edited the post and made it the full script. Asynchronize 16 — 5y
0
I think I've fixed your changes. SteelMettle1 394 — 5y
View all comments (5 more)
0
For some reason it still isn't working, it's being really weird but I don't want to bother you again with this. I've just been really busy with work so I can't work on this that much as I want too. It's been making me mad lol. I'll probably work on it later on with some of yours tips to help me. Thanks for everything. Asynchronize 16 — 5y
0
It sometimes stops playing for the 2nd half of the song, after a switch it will sometimes start a song already half way through, sometimes just switches and shows the song name but doesn't play the song. Not sure if you know what could be causing any of this? Asynchronize 16 — 5y
0
Your first problem may be from Roblox only allowing 1 minute 20 second audio files. So if you have a song that's 3 minutes long, you'll need at least 2 audio files (you'll still lose the last 20 seconds of the song though unless you play a third audio file). SteelMettle1 394 — 5y
0
As far as the second problem, I'm honestly not sure. I tested the code given in every way I could think of and it worked just fine. This tells me there's something else in your code that I don't have, unless of course it's because of the 1 minute 20 second problem. SteelMettle1 394 — 5y
0
I'm not too sure either man, thanks for your help though. Asynchronize 16 — 5y

Answer this question