myjobisgop Posted October 11, 2013 Share Posted October 11, 2013 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
pa3ck Posted October 11, 2013 Share Posted October 11, 2013 (edited) Does /debugscript 3 show any errors? Also, if you use your command then it will play the music on the current player's position. You have to use attachElements ( https://wiki.multitheftauto.com/wiki/AttachElements ) and attach the sound to the car. Edited October 11, 2013 by Guest Link to comment
manawydan Posted October 11, 2013 Share Posted October 11, 2013 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
myjobisgop Posted October 11, 2013 Author Share Posted October 11, 2013 I did as told manawydan and it works. But was appearing a new problem. The variable playerVehicle accepts only false value constantly.Why is it so? Link to comment
Castillo Posted October 11, 2013 Share Posted October 11, 2013 Because the player isn't on a vehicle? Link to comment
myjobisgop Posted October 11, 2013 Author Share Posted October 11, 2013 Yes and when player drives a vehicle! Link to comment
Castillo Posted October 11, 2013 Share Posted October 11, 2013 Post your current script. Link to comment
myjobisgop Posted October 11, 2013 Author Share Posted October 11, 2013 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
Castillo Posted October 11, 2013 Share Posted October 11, 2013 addCommandHandler client side doesn't have a player argument, so, remove that 'source' from the function name, then rename all 'source' in the rest of the script to 'localPlayer'. Link to comment
myjobisgop Posted October 11, 2013 Author Share Posted October 11, 2013 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
Castillo Posted October 11, 2013 Share Posted October 11, 2013 1: You must make a bridge between the client and the server, so, make the command server sided and trigger an event to every player to start the music. 2: You must use isElement to check if the sound is already started, if so, destroy it with stopSound or destroyElement. Link to comment
myjobisgop Posted October 11, 2013 Author Share Posted October 11, 2013 Ohh, Can you tell more about 1? Is it hard? Because i am a new in mta scripting. Link to comment
manawydan Posted October 12, 2013 Share Posted October 12, 2013 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
myjobisgop Posted October 12, 2013 Author Share Posted October 12, 2013 manawydan, Your script has started. But when i enter a command "sm", debugscript dispalays: music/music.lua:20:attempt to call global 'triggerClientEvent' (a nil value) Link to comment
Castillo Posted October 12, 2013 Share Posted October 12, 2013 Make sure the script is set as server side, the script containing the command, and the other as client side. Link to comment
myjobisgop Posted October 12, 2013 Author Share Posted October 12, 2013 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
Castillo Posted October 12, 2013 Share Posted October 12, 2013 Client side script? if so, then remove getLocalPlayer() from bindKey. Link to comment
myjobisgop Posted October 12, 2013 Author Share Posted October 12, 2013 No. it is a server side script. Link to comment
Castillo Posted October 12, 2013 Share Posted October 12, 2013 Then, you must bind the key when the player joins ( onPlayerJoin ) and for every player when the resource starts ( onResourceStart ). Link to comment
myjobisgop Posted October 13, 2013 Author Share Posted October 13, 2013 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
Castillo Posted October 13, 2013 Share Posted October 13, 2013 Remove 'source' from function name, as it's already defined by it's own. Link to comment
myjobisgop Posted October 13, 2013 Author Share Posted October 13, 2013 I made: function bindKeyHandler() bindKey(source, "0", "down", stopMusic) end addEventHandler("onPlayerJoin", root, bindKeyHandler) addEventHandler("onResourceStart", root, bindKeyHandler) but, debugscripter shows the same error. Link to comment
TAPL Posted October 13, 2013 Share Posted October 13, 2013 Because event onResourceStartdoesn't have player source, you will need to use: getElementsByType Loop all players in the server. And please use Lua tag next time, easier to read the code. Link to comment
myjobisgop Posted October 13, 2013 Author Share Posted October 13, 2013 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
TAPL Posted October 13, 2013 Share Posted October 13, 2013 addEventHandler("onResourceStart", resourceRoot, function() for i, player in ipairs (getElementsByType("player")) do bindKey(player, "0", "down", stopMusic) end end) Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now