Skip to Content
Cobalt

cobalt_sound

Server-authoritative 2D and 3D audio from files and URLs.

ExportSideWhat it does
PlayServer, ClientStarts a sound with exactly one of coords, entity or player.
PlayFrontendServer, ClientPlays a 2D sound for one player.
PlayAtServer, ClientPlays a 3D sound at a world position.
PlayOnEntityServer, ClientPlays a 3D sound that follows a networked entity.
StopSharedStops a sound; on the client only sounds that player started.
PauseSharedPauses a sound.
ResumeSharedResumes a paused sound.
SeekSharedMoves a sound's playhead.
SetVolumeSharedSets a sound's volume.
SetLoopSharedTurns looping on or off.

Play

Server

Starts a sound with exactly one of coords, entity or player.

exports.cobalt_sound:Play(data)
ParameterTypeOptionalDescription
dataSoundDataNo
ReturnsDescription
integer?id
string?error
Example
local id, error = exports.cobalt_sound:Play(
    { sound = 'cash_register', coords = vector3(25.7, -1347.3, 29.5), volume = 0.8, distance = 15.0 }
)

Client

Asks the server to start a sound; the target must be near or be the local player, and a coords sound plays in the player's routing bucket. distance is capped at ClientRange. The sound stops when the calling resource stops, and a world sound also after ClientLifetime seconds.

exports.cobalt_sound:Play(data)
ParameterTypeOptionalDescription
dataSoundDataNo
ReturnsDescription
integer | falseid
string?error
Example
local id, error = exports.cobalt_sound:Play(
    { sound = 'cash_register', coords = vector3(25.7, -1347.3, 29.5), volume = 0.8, distance = 15.0 }
)

PlayFrontend

Server

Plays a 2D sound for one player.

exports.cobalt_sound:PlayFrontend(target, sound, volume)
ParameterTypeOptionalDescription
targetintegerNoserver id, or -1 for everyone
soundstringNo
volumenumberYes0 to 1
ReturnsDescription
integer?id
string?error
Example
local id, error = exports.cobalt_sound:PlayFrontend(source, 'cash_register', 0.5)

Client

Plays a file from sounds/ locally with no id.

exports.cobalt_sound:PlayFrontend(sound, volume)
ParameterTypeOptionalDescription
soundstringNofile in sounds/, as name or name.ogg
volumenumberYes0 to 1
Example
exports.cobalt_sound:PlayFrontend('cash_register', 0.5)

PlayAt

Server

Plays a 3D sound at a world position.

exports.cobalt_sound:PlayAt(coords, sound, volume, distance)
ParameterTypeOptionalDescription
coordsvector3No
soundstringNo
volumenumberYes0 to 1
distancenumberYesmeters
ReturnsDescription
integer?id
string?error
Example
local id, error = exports.cobalt_sound:PlayAt(
    vector3(215.8, -810.1, 30.7),
    'cash_register',
    0.5,
    10.0
)

Client

Asks the server to play a 3D sound at a world position.

exports.cobalt_sound:PlayAt(coords, sound, volume, distance)
ParameterTypeOptionalDescription
coordsvector3No
soundstringNo
volumenumberYes0 to 1
distancenumberYesmeters
ReturnsDescription
integer | falseid
string?error
Example
local id, error = exports.cobalt_sound:PlayAt(
    vector3(215.8, -810.1, 30.7),
    'cash_register',
    0.5,
    10.0
)

PlayOnEntity

Server

Plays a 3D sound that follows a networked entity.

exports.cobalt_sound:PlayOnEntity(netId, sound, volume, distance)
ParameterTypeOptionalDescription
netIdintegerNo
soundstringNo
volumenumberYes0 to 1
distancenumberYesmeters
ReturnsDescription
integer?id
string?error
Example
local id, error = exports.cobalt_sound:PlayOnEntity(
    NetworkGetNetworkIdFromEntity(vehicle),
    'cash_register',
    0.5,
    10.0
)

Client

Asks the server to play a 3D sound that follows a networked entity.

exports.cobalt_sound:PlayOnEntity(netId, sound, volume, distance)
ParameterTypeOptionalDescription
netIdintegerNo
soundstringNo
volumenumberYes0 to 1
distancenumberYesmeters
ReturnsDescription
integer | falseid
string?error
Example
local id, error = exports.cobalt_sound:PlayOnEntity(
    NetworkGetNetworkIdFromEntity(vehicle),
    'cash_register',
    0.5,
    10.0
)

Stop

Shared

Stops a sound; on the client only sounds that player started.

exports.cobalt_sound:Stop(id)
ParameterTypeOptionalDescription
idintegerNo
ReturnsDescription
boolean
Example
local ok = exports.cobalt_sound:Stop(1)

Pause

Shared

Pauses a sound.

exports.cobalt_sound:Pause(id)
ParameterTypeOptionalDescription
idintegerNo
ReturnsDescription
boolean
Example
local ok = exports.cobalt_sound:Pause(1)

Resume

Shared

Resumes a paused sound.

exports.cobalt_sound:Resume(id)
ParameterTypeOptionalDescription
idintegerNo
ReturnsDescription
boolean
Example
local ok = exports.cobalt_sound:Resume(1)

Seek

Shared

Moves a sound's playhead.

exports.cobalt_sound:Seek(id, seconds)
ParameterTypeOptionalDescription
idintegerNo
secondsnumberNo
ReturnsDescription
boolean
Example
local ok = exports.cobalt_sound:Seek(1, 30.0)

SetVolume

Shared

Sets a sound's volume.

exports.cobalt_sound:SetVolume(id, volume)
ParameterTypeOptionalDescription
idintegerNo
volumenumberNo0 to 1
ReturnsDescription
boolean
Example
local ok = exports.cobalt_sound:SetVolume(1, 0.5)

SetLoop

Shared

Turns looping on or off.

exports.cobalt_sound:SetLoop(id, loop)
ParameterTypeOptionalDescription
idintegerNo
loopbooleanNo
ReturnsDescription
boolean
Example
local ok = exports.cobalt_sound:SetLoop(1, true)

On this page