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

Pausing Sound on Server does Pause the Sound for players?

Asked by 4 years ago
Edited 4 years ago

I came across this very annoying bug where I am using RemoteEvents to pause, play, stop, and resume sound on the game, which uses an if statement to separate them. The problem is that once it plays the sound, I am unable to pause or stop it once it is playing. I desperately need help on this as this is very important for me game that everyone hears the same music. If you really need the code, I'll post it on here.

Plus, I would want an answer to this as this will make me know more about lua as to do this is something I don't know yet and I am still learn Lua a bit. I know a lot about lua but I am actually still working with RemoteEvents as it seem that this game I am making seems to need them quite a bit. I also learn better when I am doing projects as well.

The game I am making is a DJ type game so that is why a lot of things are labeled with DJ inside.

Module Script ID: 1 = The Module script the script calls, this module script is quite important for future reference in my code so not many bugs are accidentally created if I forgot anything. This is also suppose to act as a sort of template as well so I could use this single line of code for a lot of different things.

Module Name: DJPlayer

function DJPlayer.MediaPlayer(Mode,TimePos,SoundId)
        local Path = game:GetService("SoundService").DJMusic.Music
        if SoundId ~= nil then
            print(SoundId)
            DJPlayer.PlayerId(SoundId)
        end
        if TimePos ~= nil then
            print(TimePos)
            Path.TimePosition = TimePos
        end
        if Mode ~= nil then
            if Mode == "Play" then
                Path:Play()
                return true
            elseif Mode == "Stop" then
                Path:Stop()
                return true
            elseif Mode == "Resume" then
                Path:Resume()
                return true
            elseif Mode == "Pause" then
                Path:pause()
                return true
            else
                error(Mode .. " is an invalid sound mode")
                return false
            end
        else
            return true
        end
    end

Script ID: 2 = The script where it detects the serverEvent

local Replicate = game:GetService("ReplicatedStorage")
local MediaEvent = Replicate:WaitForChild("MediaPlayer")
local DJPlayer = require(game.ReplicatedStorage.Modules.Game.DJPlayer)

MediaEvent.OnServerEvent:connect(function(Player,Mode,TimePos,SoundId)
    DJPlayer.MediaPlayer(Mode,TimePos,SoundId)
end)

LocalScript ID: 3 = The localscript of where the player is using. This is important for whoever the DJ of the game is. I am only going to include parts of what is more important and most other elements I have tested and worked. So only including things that is link to the problem I might have

local MediaEvent = game:GetService("ReplicatedStorage"):WaitForChild("MediaPlayer")

MediaEvent:FireServer("Stop") -- There is actually a inside a MouseButton1Click function.

Edit: I still need help with this as to why it happens.

Edit #2: Just to notify, I still need help us this is a problem I am unsure to help with. If you are a bit confused of what I am asking than please don't hesitate to ask what part you are confused about.

0
Please post the script you're using OhManXDXD 445 — 4y
0
I edited my question and added some code. jonathanpecany100 0 — 4y
0
@OhManLolLol I edited my question and added my code in it. as it might not be necessary but if you really need it than okay. jonathanpecany100 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I have finally fixed my problem and it was quite an easy fix. Just send the data back to allclients and play the sound from there. Don't really know why people couldn't answer that but okay.

I'll add the updated scripts and probably ID's to the scripts from my original answer to help determine the position and change.

Script ID: 2 = The script that will detect the server event and run the client event.

local Replicate = game:GetService("ReplicatedStorage")
local MediaEvent = Replicate:WaitForChild("MediaPlayer")
local Callback = Replicate:WaitForChild("SoundCallback")
local DJPlayer = require(game.ReplicatedStorage.Modules.Game.DJPlayer)

MediaEvent.OnServerEvent:connect(function(Player,Mode,TimePos,SoundId)
    DJPlayer.MediaPlayer(Mode,TimePos,SoundId)
    Callback:FireAllClients(Mode,TimePos,SoundId)
end)

Local Script ID: 4 = The local script of where it detects the client event.

local Callback = game:GetService("ReplicatedStorage"):WaitForChild("SoundCallback")
local DJPlayer = require(game.ReplicatedStorage.Modules.Game.DJPlayer)

Callback.OnClientEvent:connect(function(Mode,TimePos,SoundId)
    DJPlayer.MediaPlayer(Mode,TimePos,SoundId)
end)

To mention again, I only included the scripts that I changed, other scripts that I did not list here stayed the same.

Ad

Answer this question