Jump to content

Train Horn?


Recommended Posts

Hi,

I'm from Germany and i have a Problem with my Script.

I'd like a train horn create the all hear on the server.

Unfortunately, my script does not work and I can not continue to hope for your help. :dark1:

File train_horn.lua Server:

function onTrainHornUse (player, key, keyState) 
  
        local theVehicle = getPedOccupiedVehicle(player) 
        if (not theVehicle) then 
            return 
        end 
    
    if (getElementModel(theVehicle) == 537) or (getElementModel(theVehicle) == 538) then 
        if (keyState == "down") then 
            triggerClientEvent(root, "onStartTrainHorn", player) 
        elseif (keyState == "up") then 
            triggerClientEvent(root, "stopTrainHorn", player) 
        end 
    end 
end 
  
function onBindPlayerTrain(player) 
     
    bindKey(player, "h", "down", onTrainHornUse) 
    bindKey(player, "h", "up", onTrainHornUse) 
end 
addEventHandler("onPlayerSpawn", getRootElement(), onBindPlayerTrain) 

File train_horn_sound.lua Client:

function onStartTrainHorn(player) 
  
    if (player) then 
        local theVehicle = getPedOccupiedVehicle(player) 
        if (not theVehicle) then 
            return 
        end 
        local x, y, z = getElementPosition(player) 
        getTrainSound = playSound3D("addons/trainhorn/trainhorn.wav", x, y, z) 
        setSoundVolume(getTrainSound, 1.0) 
        setSoundMaxDistance(getTrainSound, 190) 
        attachElements(getTrainSound, theVehicle) 
    end 
end 
addEvent("onStartTrainHorn", true) 
addEventHandler("onStartTrainHorn", getRootElement(), onStartTrainHorn) 
             
function stopTrainHorn() 
  
    if isElement(getTrainSound) then 
        stopSound(getTrainSound) 
    end 
end 
addEvent("stopTrainHorn", true) 
addEventHandler("stopTrainHorn", getRootElement(), stopTrainHorn) 

Now I wait for an answer.

Link to comment

Try this it should work

-- Server side

  
  
function onTrainHornUse (player, key, keyState) 
    if isPedInVehicle (player) then  
        local theVehicle = getPedOccupiedVehicle(player) 
        if (getElementModel(theVehicle) == 537) or (getElementModel(theVehicle) == 538) then 
        if (keyState == "down") then 
            triggerClientEvent(root,"onStartTrainHorn",root,theVehicle) 
        elseif (keyState == "up") then 
            triggerClientEvent(root, "stopTrainHorn", root) 
            end 
        end 
    end 
end  
  
function onBindPlayerTrain(player) 
    bindKey(player, "h", "down", onTrainHornUse) 
    bindKey(player, "h", "up", onTrainHornUse) 
end 
  
  
function bindHorn () 
    onBindPlayerTrain(source) 
end 
addEventHandler("onPlayerJoin",getRootElement(), bindHorn) 
  
function Train () 
        for index, player in pairs(getElementsByType("player")) do 
            onBindPlayerTrain(player) 
        end 
end 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), Train) 

-- Client side

function onStartTrainHorn(vehicle) 
    local x, y, z = getElementPosition(localPlayer) 
    getTrainSound = playSound3D("addons/trainhorn/trainhorn.wav", x, y, z) 
    setSoundVolume(getTrainSound, 1.0) 
    setSoundMaxDistance(getTrainSound, 190) 
    attachElements(getTrainSound, vehicle) 
end 
addEvent("onStartTrainHorn", true) 
addEventHandler("onStartTrainHorn", getRootElement(), onStartTrainHorn) 
            
function stopTrainHorn() 
    if isElement(getTrainSound) then 
        stopSound(getTrainSound) 
    end 
end 
addEvent("stopTrainHorn", true) 
addEventHandler("stopTrainHorn", getRootElement(), stopTrainHorn) 

Link to comment

You don't need to bind the key on both up and down, the idea is actually relatively simple. You'll manage the horn on the server side then you'll pass the train element to all nearby clients using triggerClientEvent, on the client you can attach the sound to the train and set the sound max distance. Train horn has been within the GTW-RPG project for a long time as well, take a look for more inspiration:

https://github.com/GTWCode/GTW-RPG/tree/master/%5Bresources%5D/GTWtrainhorn. And good luck, train horns are a pretty nice feature. :wink:

Link to comment
Hi,

I'm from Germany and i have a Problem with my Script.

I'd like a train horn create the all hear on the server.

Unfortunately, my script does not work and I can not continue to hope for your help. :dark1:

File train_horn.lua Server:

function onTrainHornUse (player, key, keyState) 
  
        local theVehicle = getPedOccupiedVehicle(player) 
        if (not theVehicle) then 
            return 
        end 
    
    if (getElementModel(theVehicle) == 537) or (getElementModel(theVehicle) == 538) then 
        if (keyState == "down") then 
            triggerClientEvent(root, "onStartTrainHorn", player) 
        elseif (keyState == "up") then 
            triggerClientEvent(root, "stopTrainHorn", player) 
        end 
    end 
end 
  
function onBindPlayerTrain(player) 
     
    bindKey(player, "h", "down", onTrainHornUse) 
    bindKey(player, "h", "up", onTrainHornUse) 
end 
addEventHandler("onPlayerSpawn", getRootElement(), onBindPlayerTrain) 

File train_horn_sound.lua Client:

function onStartTrainHorn(player) 
  
    if (player) then 
        local theVehicle = getPedOccupiedVehicle(player) 
        if (not theVehicle) then 
            return 
        end 
        local x, y, z = getElementPosition(player) 
        getTrainSound = playSound3D("addons/trainhorn/trainhorn.wav", x, y, z) 
        setSoundVolume(getTrainSound, 1.0) 
        setSoundMaxDistance(getTrainSound, 190) 
        attachElements(getTrainSound, theVehicle) 
    end 
end 
addEvent("onStartTrainHorn", true) 
addEventHandler("onStartTrainHorn", getRootElement(), onStartTrainHorn) 
             
function stopTrainHorn() 
  
    if isElement(getTrainSound) then 
        stopSound(getTrainSound) 
    end 
end 
addEvent("stopTrainHorn", true) 
addEventHandler("stopTrainHorn", getRootElement(), stopTrainHorn) 

Now I wait for an answer.

According to your scripts, only local player will hear your sound (whose location is close to you in the game.), if you want everyone on the server to hear this, you should trigger playSound for every player in the server

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...