Jump to content

Same questions


myjobisgop

Recommended Posts

Hi. I have a some questions

1)For example I get the element(player, vehicle, Sound) in server side script and transfer in client function with triggerClientEvent. Can i change that element in client side(attachElement, setElementData, deleteElement, etc..)??

2)For example I get the element (player, vehicle, Sound) in function1 on server or client side and transfer their in function1. Can i change that element in function1(attachElement, setElementData, deleteElement, etc..)??

Link to comment

Solidsnake14, for example i show my code:

In server side

  
function ResourceStart() 
local playerVehicle = getPedOccupiedVehicle(source); 
  
triggerClientEvent(getRootElement(), "ClientEvent", source, playerVehicle) 
  
end 
  

In client side

  
function ClientEventHandler(playerVehicle) 
     local Sound = playSound3D('track.mp3', 1, 2, 3, true) 
  
     StartMusic(Sound, playerVehicle) 
end 
  
function StartMusic(Sound, playerVehicle) 
    attachElement(Sound, playerVehicle, ) 
end 
  

Sound creates, but it plays only in fixed coordinates (1,2,3). Is it mean that Sound not exist in StartMusic function?

Link to comment

Because the code makes no sense.

Client-side

function doSound(vehicle) 
 -- Check if argument is set and whether it is a vehicle element 
 if (not vehicle) or (not isElement(vehicle)) or (getElementType(vehicle) ~= "vehicle") then 
  outputChatBox("Invalid vehicle argument or element.", 245, 20, 20, false) 
  return 
 end 
  
 local x, y, z = getElementPosition(vehicle) -- Get the position of the vehicle 
 local sound = playSound3D("path.mp3", x, y, z, true) -- Play the sound looped at the position 
 setSoundMinDistance(sound, 7) -- Set the sound's minimum distance to 7 units 
 attachElements(sound, vehicle) -- Attach the sound to the vehicle 
end 
addEvent("triggerClientDoSound", true) 
addEventHandler("triggerClientDoSound", root, doSound) 
  
function endSound(vehicle) 
 -- Check if argument is set and whether it is a vehicle element 
 if (not vehicle) or (not isElement(vehicle)) or (getElementType(vehicle) ~= "vehicle") then 
  outputChatBox("Invalid vehicle argument or element.", 245, 20, 20, false) 
  return 
 end 
  
 for _,element in ipairs(getAttachedElements(vehicle)) do -- Get all attached elements 
  if (getElementType(element) == "sound") then -- Check if it's a sound 
   destroyElement(element) -- Destroy the sound 
  end 
 end 
end 
addEvent("triggerClientEndSound", true) 
addEventHandler("triggerClientEndSound", root, endSound) 
  
addEventHandler("onClientResourceStart", resourceRoot, 
 function() 
  for _,vehicle in ipairs(getElementsByType("vehicle")) do -- Loop through all vehicles 
   doSound(vehicle) -- Trigger the sound 
  end 
 end 
) 

If you ever wanted to trigger it server-side then just trigger the custom event with 'root' as the source. Make sure you include the vehicle argument.

So, the sound start function is doSound and stopping it is endSound.

Link to comment

myonlake, i changed my code and now it has the next view:

Server-side

  
function setTrackInMP(source, command, trackNumber) 
    local playerVehicle = getPedOccupiedVehicle(source); 
    local MPMaxTracks = tonumber(get("MPMaxTracks")) --This setting is in meta.xml 
    
    if ((playerVehicle ~= false) and (source == getVehicleOccupant(playerVehicle))) then 
  
        if (trackNumber ~= '') then    
            local TN = tonumber(trackNumber) 
             
            if  (TN >= 1 and TN < MPMaxTracks) then 
                local pvX, pvY, pvZ = getElementPosition(playerVehicle) 
  
                triggerClientEvent(getRootElement(), "MPChange", source, playerVehicle, TN, pvX, pvY, pvZ) 
                 
                playerVehicle, MPMaxTracks = nil 
            else 
                outputChatBox("Track number enters incorrect!", source, 255, 0, 0, true) 
            end 
        else 
            outputChatBox("You didn't enter a track number!", source, 255, 0, 0, true) 
        end 
    else 
        outputChatBox("You don't turn a music player if you are not drive the vehicle!", source, 255, 0, 0, true) 
    end 
end 
  
addCommandHandler("sm", setTrackInMP) 
  
addEvent("MPErrorOutput", true) 
  
function MPErrorOutputHandler(functionName, ErrorCode) 
    local logFilenName = get("MPLogFileName") 
     
    local LogFile = io.open(logFilenName) 
     
    if LogFile then 
        LogFile:write("["..os.date("%Y-%m-%d %H:%M:%S").."] PLAYER: "..getPlayerName(source).." , Error in function '"..functionName.."' , ErrorCode: "..ErrorCode.."\n") 
        LogFile:close() 
    end 
     
    logFilenName = nil 
     
    restartResource(getThisResource()) 
end 
  
addEventHandler("MPErrorOutput", getRootElement(), MPErrorOutputHandler) 
  
 

Client-side

  
addEvent("MPChange", true) 
  
function MPChangeHandler(playerVehicle, trackNumber, pvX, pvY, pvZ) 
    local ErrorCode = 0 
     
    if (playerVehicle ~= false) then 
        if(getElementData(playerVehicle, "MPTurnOn") == 1) then 
            if (MPStopTrack(playerVehicle) ~= 0) then 
                ErrorCode = 2 
            end 
        else 
            if not(setElementData (playerVehicle, "MPTurnOn", 1)) then 
                ErrorCode = 3 
            end 
        end 
         
        if (ErrorCode == 0) then 
            local Sound = playSound3D('music_tracks/'..trackNumber..'.mp3', pvX, pvY, pvZ, true) 
         
            if (Sound ~= false) then 
                if setElementData (Sound, "MPSoundElement", 1) then 
                    setSoundMaxDistance(Sound, 20) 
                    setSoundVolume(Sound, 0.5) 
                     
                    if not(MPStartTrack(Sound, playerVehicle)) then 
                        destroyElement(Sound) 
                        ErrorCode = 6 
                        triggerServerEvent("MPErrorOutput", "MPChangeHandler", source, ErrorCode) 
                    else 
                        outputChatBox("Track #"..trackNumber.." is now playing in a music player.", source, 0, 196, 58, true) 
                         
                    end 
                else 
                    ErrorCode = 5 
                    triggerServerEvent("MPErrorOutput", "MPChangeHandler", source, ErrorCode) 
                end 
            else 
                ErrorCode = 4 
                triggerServerEvent("MPErrorOutput", "MPChangeHandler", source, ErrorCode) 
            end 
        else 
            triggerServerEvent("MPErrorOutput", "MPChangeHandler", source, ErrorCode) 
        end 
    else 
        ErrorCode = 1 
        triggerServerEvent("MPErrorOutput", source, "MPChangeHandler", ErrorCode) 
    end 
end 
  
function MPStartTrack(MPSoundElement, playerVehicle) 
    local ErrorCode = 0 
  
    if setElementData (MPSoundElement, "MPSoundElement", 1) then 
        setSoundMaxDistance(MPSoundElement, 20) 
        setSoundVolume(MPSoundElement, 0.5) 
                         
        if setElementData (playerVehicle, "MPSoundElement", MPSoundElement) then 
            ErrorCode = 2 
        else 
            if not(attachElements(MPSoundElement, playerVehicle, 0, 0, 0)) then 
                ErrorCode = 3 
            end 
        end 
    else 
        ErrorCode = 1 
    end 
     
    return ErrorCode 
end 
  
function MPStopTrack(playerVehicle) 
    local ErrorCode = 0 
    local playerVehicleAttachedElements = getAttachedElements(playerVehicle) 
     
    if not(playerVehicleAttachedElements) then 
        ErrorCode = -1 
    else 
        for pvaeKey, pvaeValue in ipairs (playerVehicleAttachedElements) do 
            if (getElementData(pvaeValue, "MPSoundElement") == 1) then 
                destroyElement(pvaeValue) 
                break 
            end 
        end 
  
        playerVehicleAttachedElements, pvaeKey, pvaeValue = nil 
         
        if not(removeElementData(playerVehicle, "MPSoundElement")) then 
            ErrorCode = -2 
        end 
    end 
     
    return ErrorCode 
end 
  
 

But it plays sound only in fixed coordinates again.What did I do wrong?

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...