Round Beginning Sounds

This code doesn’t emit any sounds. Does anyone have a clue why? Pre-thanks :slight_smile:

It’s supposed to play a specific sound for each player based on their role.


--Config start--
local sounds = {
innocent = "roundsounds/innocent_start.mp3",
detective = "roundsounds/detective_start.mp3",
traitor = "roundsounds/traitor_start.mp3"
}
--Config end  --

for k, v in pairs( sounds ) do
util.PrecacheSound( "sound/".. v )
end

if SERVER then
util.AddNetworkString( "RoundStartSound" )
for k, v in pairs( sounds ) do
resource.AddFile( "sound/"..v )
end

hook.Add("RoundStateChange", "RoundStartSound", function(state, statetoo)
if state != ROUND_ACTIVE and statetoo != ROUND_ACTIVE then return end
net.Start( "RoundStartSound" )
net.Broadcast()
end )
else
local role = nil
local client = LocalPlayer()

net.Receive("RoundStartSound", function()
if client:GetTraitor() then
role = "traitor"
elseif client:GetDetective() then
role = "detective"
else
role = "innocent"
end
surface.PlaySound( sounds[role] )
end )
end

Is that all one file?

Correct me if I’m wrong, but it looks like he’s trying to get the path of the sound via a string index, when his table actually has numerical indexes.
He’s just assigning variables inside the table rather than assigning string indexes to the values.

Yes it’s all one file, named game_sounds.lua, put into lua/autorun/

Try changing your config table to…
[lua]
local sounds = { }
sounds[“innocent”] = “roundsounds/innocent_start.mp3”
sounds[“detective”] = “roundsounds/detective_start.mp3”
sounds[“traitor”] = “roundsounds/traitor_start.mp3”
[/lua]

Also, he’s not using the net library correctly. There is no clientside code.

Is that a difficult thing to set up?

Try this:


--Config start--
local sounds = {
    ["innocent"] = "roundsounds/innocent_start.mp3",
    ["detective"] = "roundsounds/detective_start.mp3",
    ["traitor"] = "roundsounds/traitor_start.mp3"
}
--Config end  --

for k, v in pairs( sounds ) do
    util.PrecacheSound( "sound/".. v )
end

if SERVER then
    util.AddNetworkString( "RoundStartSound" )

    for k, v in pairs( sounds ) do
        resource.AddFile( "sound/"..v )
    end


    hook.Add("RoundStateChange", "RoundStartSound", function(state, statetoo)
        if state ~= ROUND_ACTIVE and statetoo ~= ROUND_ACTIVE then return end
        net.Start( "RoundStartSound" )
        net.Broadcast()
    end )

end

if CLIENT then

    local role
    local client = LocalPlayer()

    net.Receive("RoundStartSound", function()
        if client:GetTraitor() then
            role = "traitor"
        elseif client:GetDetective() then
            role = "detective"
        else
            role = "innocent"
        end
    
        surface.PlaySound( sounds[role] )
    
    end )
end

Thanks for trying. :downs: No sound emitted though :tinfoil:

You’ve sent those sounds to the client, right? As a check, try this and tell me if anything prints in chat when the round ends.


--Config start--
local sounds = {
    ["innocent"] = "roundsounds/innocent_start.mp3",
    ["detective"] = "roundsounds/detective_start.mp3",
    ["traitor"] = "roundsounds/traitor_start.mp3"
}
--Config end  --

for k, v in pairs( sounds ) do
    util.PrecacheSound( "sound/".. v )
end

if SERVER then
    util.AddNetworkString( "RoundStartSound" )

    for k, v in pairs( sounds ) do
        resource.AddFile( "sound/"..v )
    end


    hook.Add("RoundStateChange", "RoundStartSound", function(state, statetoo)
        if state ~= ROUND_ACTIVE and statetoo ~= ROUND_ACTIVE then return end
        net.Start( "RoundStartSound" )
        net.Broadcast()
    end )

end

if CLIENT then

    local role
    local client = LocalPlayer()

    net.Receive("RoundStartSound", function()
        if client:GetTraitor() then
            role = "traitor"
        elseif client:GetDetective() then
            role = "detective"
        else
            role = "innocent"
        end
    
        LocalPlayer():ChatPrint( "Sound played!" )
        surface.PlaySound( sounds[role] )
    
    end )
end

Yes, they download properly into download/sound/roundsounds/file.mp3

And now that I think about it, it says local sounds… isn’t the folder named SOUND?


EDIT: Nothing pops up

That means the hook was never run. I’m not familiar with the RoundStateChange hook; can you try replacing it with this?


--Config start--
local sounds = {
    ["innocent"] = "roundsounds/innocent_start.mp3",
    ["detective"] = "roundsounds/detective_start.mp3",
    ["traitor"] = "roundsounds/traitor_start.mp3"
}
--Config end  --

for k, v in pairs( sounds ) do
    util.PrecacheSound( "sound/".. v )
end

if SERVER then

    for k, v in pairs( sounds ) do
        resource.AddFile( "sound/"..v )
    end

end

if CLIENT then

    local role
    local client = LocalPlayer()

    hook.Add( "TTTEndRound", "EndRoundSound", function()
        if client:GetTraitor() then
            role = "traitor"
        elseif client:GetDetective() then
            role = "detective"
        else
            role = "innocent"
        end
    
        LocalPlayer():ChatPrint( "Sound played!" )
        surface.PlaySound( sounds[role] )
    
    end )
end

Thank you gode_gs… It didn’t work, however you always seem to be the one helping the most around here :slight_smile:

While doing this I also had a job up on coderhire (because I was desperate to get this done by the end of the day), and it happened quickly. Turns out it’s so much simpler and smaller of a code. But you guys were right about needing to do it for the client as well.

/solves :rock:

Well, just tested this on a local server and it worked fine; no idea what your issue was with it, but glad you got it solved.

Maybe a server restart was required. Which is unusual because these types of codes don’t usually require a restart for me.

Just so you know his lua was valid. It is a nice little feature with lua so you don’t have to type out the brackets and make it a string. See: http://lua-users.org/wiki/TablesTutorial