This code doesn’t emit any sounds. Does anyone have a clue why? Pre-thanks
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
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.
--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
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
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
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.
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