Jump to content

Problems with my script


myjobisgop

Recommended Posts

Good evening.I have a problem with my script

This script must to play music, when the player drives vehicle. It works the next rule: When player in vehicle, he writes in a chat a command

/sm [track number]

and later in his car starts to play music track with number [track number]

I did this:

music.lua

  
function setMusicForPlayer(source, command, trackNumber)  
         --Load player vehicle 
  local playerVehicle = getPedOccupiedVehicle(source);  
    
         --Is player driving a car now? 
  if playerVehicle then  
                 --Is player entering a track number. 
   if (trackNumber ~= nil) then  
    TN = tonumber(trackNumber)  
      
                         --Is value of track number being a natural number and >= 1 and < 3.    
    if ((TN ~= nil) and (TN >= 1 AND TN < 3)) then  
                    --Get the coordinates of player vehicle 
     local pvX, pvY, pvZ = getElementPosition(playerVehicle)  
                    --Load sound file 
     local Sound = playSound3D( 'music_tracks/'..TN..'.mp3', pvX, pvY, pvZ)  
       
                    --Check for successfull load. 
     if (Sound ~= false) then  
      setSoundMaxDistance(Sound, 100);  
      outputChatBox("Track #"..TN.." is now playing in a music player.", source, 255, 0, 0, true)  
     else  
      outputChatBox("Error loading tracks", source, 255, 0, 0, true)  
     end  
    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  
   local pvX, pvY, pvZ = getElementPosition(playerVehicle)  
  else  
   outputChatBox("You don't turn a music player if you are not in the vehicle!", source, 255, 0, 0, true)  
  end  
 end  
  
 addCommandHandler("sm", setMusicForPlayer) 
  

meta.xml

  
<meta>  
  <info description="Music" version="1.0" type="script"/>  
  <script src="music.lua" type="client"/>  
 </meta> 
  

music_track - folder for mp3 tracks.

Script loads successfully in a server. But when i enter a comand /sm [track_number] nothing happens.Even if a player drives the vehicle then too there is no response.

Why is it so?

Link to comment

try

function setMusicForPlayer(source, command, trackNumber) 
         --Load player vehicle 
  local playerVehicle = getPedOccupiedVehicle(source); 
    
         --Is player driving a car now? 
  if playerVehicle then 
                 --Is player entering a track number. 
   if (trackNumber ~= nil) then 
    TN = tonumber(trackNumber) 
      
                         --Is value of track number being a natural number and >= 1 and < 3.   
    if ((TN ~= nil) and (TN >= 1 and TN < 3)) then 
                    --Get the coordinates of player vehicle 
     local pvX, pvY, pvZ = getElementPosition(playerVehicle) 
                    --Load sound file 
     local Sound = playSound3D( 'music_tracks/'..TN..'.mp3', pvX, pvY, pvZ) 
     attachElements(Sound,source,0,0,0) 
                    --Check for successfull load. 
     if (Sound) then 
      setSoundMaxDistance(Sound, 100); 
      outputChatBox("Track #"..TN.." is now playing in a music player.", source, 255, 0, 0, true) 
     else 
      outputChatBox("Error loading tracks", source, 255, 0, 0, true) 
     end 
    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 
   local pvX, pvY, pvZ = getElementPosition(playerVehicle) 
  else 
   outputChatBox("You don't turn a music player if you are not in the vehicle!", source, 255, 0, 0, true) 
  end 
 end 
  
 addCommandHandler("sm", setMusicForPlayer) 

Link to comment

My script contains 2 files and 1 folder for music

meta.xml

music.lua

music_tracks - Folder for mp3 tracks.

meta.xml

  
<meta> 
    <info author="myjobisgop" description="Music" version="1.0" type="script"/> 
    <script src="music.lua" type="shared"/> 
    <file src="music_tracks/1.mp3" download="true"/> 
              <file src="music_tracks/2.mp3" download="true"/> 
</meta> 
  

music.lua

  
function setMusicForPlayer(source, command, trackNumber) 
    --Load player vehicle 
    local playerVehicle = getPedOccupiedVehicle(source); 
    
    --Is player driving a car now? 
    if playerVehicle then 
         
        --Is player entering a track number. 
        if (trackNumber ~= '') then    
            local TN = tonumber(trackNumber) 
             
            --Is value of track number being a natural number and >= 1 and < 3.   
            if  (TN >= 1 and TN < 3) then 
                 
                --Get the coordinates of player vehicle 
                local pvX, pvY, pvZ = getElementPosition(playerVehicle) 
                 
                --Load sound file 
                local Sound = playSound3D( 'music_tracks/'..TN..'.mp3', pvX, pvY, pvZ) 
                 
                --Check for successfull load. 
                if (Sound) then 
                    attachElements(Sound,source,0,0,0) 
                    setSoundMaxDistance(Sound, 100); 
                    outputChatBox("Track #"..TN.." is now playing in a music player.", source, 255, 0, 0, true) 
                else 
                    outputChatBox("Error loading tracks", source, 255, 0, 0, true) 
                end 
            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 in the vehicle!", source, 255, 0, 0, true) 
    end 
 end 
  
 addCommandHandler("sm", setMusicForPlayer) 
  

Link to comment

Yes. Now script is working. But:

1)Music hears only the player who runs the command. And I wanted the music heard all players that are near the player. How can it do?

2)If a player re-enters the command "sm", then the music starts to overlap the previous one. How can this be solved?

Link to comment

try no tested

--server 
function setMusicForPlayer(source, command, trackNumber) 
    --Load player vehicle 
    local playerVehicle = getPedOccupiedVehicle(source); 
    
    --Is player driving a car now? 
    if playerVehicle then 
        
        --Is player entering a track number. 
        if (trackNumber ~= '') then   
            local TN = tonumber(trackNumber) 
            
            --Is value of track number being a natural number and >= 1 and < 3.   
            if  (TN >= 1 and TN < 3) then 
                
                --Get the coordinates of player vehicle 
                local pvX, pvY, pvZ = getElementPosition(playerVehicle) 
                
                 
               Sound = triggerClientEvent(root,"StartSounds",source,TN,pvX,pvY,pvZ) 
                --Check for successfull load. 
                if (Sound) then 
                    attachElements(Sound,source,0,0,0) 
                     
                    outputChatBox("Track #"..TN.." is now playing in a music player.", source, 255, 0, 0, true) 
                else 
                    outputChatBox("Error loading tracks", source, 255, 0, 0, true) 
                end 
            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 in the vehicle!", source, 255, 0, 0, true) 
    end 
 end 
  
 addCommandHandler("sm", setMusicForPlayer) 
  
 --client 
  
 addEvent("StartSounds",true) 
  addEventHandler("StartSounds",root, 
  function(track,pvX, pvY, pvZ) 
  if not(Sound) then 
  local Sound = playSound3D( 'music_tracks/'..track..'.mp3', pvX, pvY, pvZ) 
  setSoundMaxDistance(Sound, 100) 
  return Sound end 
  end) 
   
   

Link to comment

So, Now I want that If player turn music, he can turn off music using the 0 button.

I wrote a script in server side as:

server.lua

  
function stopMusic(thePlayer, key, keyState) 
    local playerVehicle = getPedOccupiedVehicle(thePlayer); 
     
    if (playerVehicle ~= false) then 
        local playerVehicleAttachedElements = getAttachedElements(playerVehicle) 
         
        if playerVehicleAttachedElements then 
            for ElementKey, ElementValue in ipairs (playerVehicleAttachedElements) do 
                if (getElementData(ElementValue, "MusicPlayer") == 1) then 
                    destroyElement(ElementValue); 
                    break; 
                end 
            end 
            outputChatBox("Music player off!", thePlayer, 255, 0, 0, true) 
            return 0 
        else 
            return (-2) 
        end 
    else 
        return (-1) 
    end 
end 
  
bindKey(getLocalPlayer(), "0", "down", stopMusic) 
  

But when i press the 0 button - then nothing happens.Why is it so?

Link to comment

Solidsnake14, I did so:

 --Server side code 
function stopMusic(thePlayer, key, keyState) 
    ... 
end 
  
function bindKeyHandler(source) 
    bindKey(source, "0", "down", stopMusic) 
end 
  
addEventHandler("onPlayerJoin", root, bindKeyHandler) 
addEventHandler("onResourceStart", root, bindKeyHandler) 

But now /debugscript 3 shows "Bad 'player' pointer @' bindKey(1)"

Why?

Link to comment

If I understand you correctly, i must do so:

  
function stopMusic(thePlayer, key, keyState) 
    local playerVehicle = getPedOccupiedVehicle(thePlayer); 
     
    if (playerVehicle ~= false) then 
        local playerVehicleAttachedElements = getAttachedElements(playerVehicle) 
         
        if playerVehicleAttachedElements then 
            for ElementKey, ElementValue in ipairs (playerVehicleAttachedElements) do 
                if (getElementData(ElementValue, "MusicPlayer") == 1) then 
                    destroyElement(ElementValue); 
                    break; 
                end 
            end 
            outputChatBox("0!", thePlayer, 255, 0, 0, true) 
            return 0 
        else 
            outputChatBox("-2!", thePlayer, 255, 0, 0, true) 
            return (-2) 
        end 
    else 
        outputChatBox("-1!", thePlayer, 255, 0, 0, true) 
        return (-1) 
    end 
end 
  
addEventHandler("onPlayerJoin", root, function() 
    bindKey(source, "0", "down", stopMusic) 
end) 
  
addEventHandler("onResourceStart", root, function() 
    bindKey(getElementsByType("player"), "0", "down", stopMusic) 
end) 
  

It is right?

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