OnClientSoundBeat: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 1: Line 1:
{{Client event}}
{{Client event}}
{{Needs Example}}
__NOTOC__  
__NOTOC__  
{{New feature|3.0131|1.3.1|
{{New feature|3.0131|1.3.1|
Line 20: Line 19:


==Example==  
==Example==  
<section name="Client" class="client" show="true">
This code will change the vehicle color to a random value if the sound beats and the localPlayer is inside a vehicle
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Not done yet
function playMySound()
    playSound("sound.mp3") -- play the sound used for onClientSoundBeat
    addEventHandler("onClientSoundBeat", getRootElement(), changeVehicleColorOnSoundBeat)
end
addEventHandler("onClientResourceStart", getRootElement(), playMySound)
 
function changeVehicleColorOnSoundBeat()
    if getPedOccupiedVehicle(localPlayer) then -- if the player is inside a vehicle
    local randomcolor = math.random(0,255) -- generate a random color value
    setVehicleColor( getPedOccupiedVehicle(localPlayer), randomcolor, randomcolor, randomcolor ) -- apply the color to the vehicle
    outputChatBox("The color of your vehicle was changed.")
    else
    outputChatBox("Could not change the vehicle color, the localPlayer is not inside a vehicle.")
    return end;
end
</syntaxhighlight>
</syntaxhighlight>
</section>


==Requirements==
==Requirements==

Revision as of 17:49, 27 August 2023

This event is triggered when a sound beats.

[[{{{image}}}|link=|]] Note: This event does not work correctly pre 1.3.1-9-04627
[[{{{image}}}|link=|]] Note: This event is triggered ahead of a sound beat the number passed is the play time at which the beat occurs

Parameters

double theTime
  • theTime: the position in the song of the beat

Source

The source of this event is the sound's element.

Example

Click to collapse [-]
Client

This code will change the vehicle color to a random value if the sound beats and the localPlayer is inside a vehicle

 function playMySound()
    playSound("sound.mp3") -- play the sound used for onClientSoundBeat
    addEventHandler("onClientSoundBeat", getRootElement(), changeVehicleColorOnSoundBeat)
end
addEventHandler("onClientResourceStart", getRootElement(), playMySound)

function changeVehicleColorOnSoundBeat()
    if getPedOccupiedVehicle(localPlayer) then -- if the player is inside a vehicle
    local randomcolor = math.random(0,255) -- generate a random color value
    setVehicleColor( getPedOccupiedVehicle(localPlayer), randomcolor, randomcolor, randomcolor ) -- apply the color to the vehicle
    outputChatBox("The color of your vehicle was changed.") 
    else 
    outputChatBox("Could not change the vehicle color, the localPlayer is not inside a vehicle.") 
    return end;
end

Requirements

This template will be deleted.

See Also

Client sound events


Client event functions