ShiDiNips 0 Posted October 9 (edited) Hello guys, new here.. So I was making this script where it plays a sound on a location.. Let's say it's' 07:00 on game it plays "daytime.mp3" and when it's 22:00 on game it plays "nighttime.mp3". Here's the code I made in clientside function daytime() local hour, minute = getTime() if (hour == 07 and minute == 00) then sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound , 1000) setSoundPanningEnabled (sound , false) end end Thanks in advance I already tried function daytime() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute if (hours == 07 and minutes == 00) then sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound , 1000) setSoundPanningEnabled (sound , false) end end Still the same, it doesn't work. I also did outputDebugString(realtime) outputDebugString(hour, minute) it shows nil Edited October 9 by ShiDiNips Share this post Link to post
majqq 88 Posted October 9 (edited) 16 minutes ago, ShiDiNips said: Hello guys, new here.. So I was making this script where it plays a sound on a location.. Let's say it's' 07:00 on game it plays "daytime.mp3" and when it's 22:00 on game it plays "nighttime.mp3". Here's the code I made in clientside function daytime() local hour, minute = getTime() if (hour == 07 and minute == 00) then sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound , 1000) setSoundPanningEnabled (sound , false) end end Thanks in advance I already tried function daytime() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute if (hours == 07 and minutes == 00) then sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound , 1000) setSoundPanningEnabled (sound , false) end end Still the same, it doesn't work. I also did outputDebugString(realtime) outputDebugString(hour, minute) it shows nil Are you sure that you call this function? Try to change 07 to 7 and 00 to 0 Code looks fine, use: https://wiki.multitheftauto.com/wiki/SetTimer Edited October 9 by majqq Share this post Link to post
ShiDiNips 0 Posted October 9 Just now, majqq said: Are you sure that you call this function? Code looks fine, use: https://wiki.multitheftauto.com/wiki/SetTimer If you meant like.. "onResourceStart", I already did that.. Also, isn't setTimer will only play something if the time set to setTimer is finished? That's why I was hoping that I could play sound using the game time Share this post Link to post
majqq 88 Posted October 9 Just now, ShiDiNips said: If you meant like.. "onResourceStart", I already did that.. Also, isn't setTimer will only play something if the time set to setTimer is finished? That's why I was hoping that I could play sound using the game time You can set timer to 1 minute, and every 1 minute it will call your function, also you can't play sounds server-side. You need to use triggerClientEvent to play sound from server to player/s. Share this post Link to post
ShiDiNips 0 Posted October 9 1 minute ago, majqq said: You can set timer to 1 minute, and every 1 minute it will call your function, also you can't play sounds server-side. You need to use triggerClientEvent to play sound from server to player/s. You mean like this? Client: function playdaysound() sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound , 1000) setSoundPanningEnabled (sound , false) end addEvent("playdemsound", true) addEventHandler ( "playdemsound", getLocalPlayer(), playdaysound ) Server: setTimer(function daytime() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute if hours == 07 and minutes == 00 then triggerClientEvent("playdemsound") end end, 1, source) Share this post Link to post
majqq 88 Posted October 9 1 minute ago, ShiDiNips said: You mean like this? Client: function playdaysound() sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound , 1000) setSoundPanningEnabled (sound , false) end addEvent("playdemsound", true) addEventHandler ( "playdemsound", getLocalPlayer(), playdaysound ) Server: setTimer(function daytime() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute if hours == 07 and minutes == 00 then triggerClientEvent("playdemsound") end end, 1, source) Yes, something like this. I also fixed your code a bit. -- Serverside function timeFunction() local realtime = getRealTime() local hours = realtime.hour local minute = realtime.minute if hours == 7 and minutes == 0 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) end end setTimer(timeFunction, 60000, 0) -- 60 000 = 1 minute, 0 = infinite times -- Clientside function onClientPlaySound() local x, y, z = 0, 0, 0 -- change your coordinates here local sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound, 1000) setSoundPanningEnabled(sound, true) end addEvent("onClientPlaySound", true) addEventHandler("onClientPlaySound", resourceRoot, onClientPlaySound) Share this post Link to post
ShiDiNips 0 Posted October 9 6 minutes ago, majqq said: Yes, something like this. I also fixed your code a bit. -- Serverside function timeFunction() local realtime = getRealTime() local hours = realtime.hour local minute = realtime.minute if hours == 7 and minutes == 0 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) end end setTimer(timeFunction, 60000, 0) -- 60 000 = 1 minute, 0 = infinite times -- Clientside function onClientPlaySound() local x, y, z = 0, 0, 0 -- change your coordinates here local sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound, 1000) setSoundPanningEnabled(sound, true) end addEvent("onClientPlaySound", true) addEventHandler("onClientPlaySound", resourceRoot, onClientPlaySound) Weirdly enough, still doesn't work and no bugs were found in /debugscript 3 Share this post Link to post
majqq 88 Posted October 9 Just now, ShiDiNips said: Weirdly enough, still doesn't work and no bugs were found in /debugscript 3 Add outputDebugString inside timeFunction, and also in condition which triggersClientEvent. https://wiki.multitheftauto.com/wiki/OutputDebugString To check, for sure. Share this post Link to post
ShiDiNips 0 Posted October 9 Just now, majqq said: Add outputDebugString inside timeFunction, and also in condition which triggersClientEvent. https://wiki.multitheftauto.com/wiki/OutputDebugString To check, for sure. realtime says nil hours says nil minutes says nil Share this post Link to post
majqq 88 Posted October 9 Just now, ShiDiNips said: realtime says nil hours says nil minutes says nil Show me your code, i guess that you call them below declaring them as variables or not exactly in this function. Share this post Link to post
ShiDiNips 0 Posted October 9 1 minute ago, majqq said: Show me your code, i guess that you call them below declaring them as variables or not exactly in this function. -- client local locsfortown:~ = { {-86.4970703125, 1120.2216796875, 0}, {-1475.625, 2629.337890625, 0}, {-2445.4267578125, 2388.9111328125, 0} } for i=1 ,#locsfortown:~ do function onClientPlaySound() xtown, ytown, ztown = locsfortown:~[i][1], locsfortown:~[i][2], locsfortown:~[i][3] ambtown = playSound3D("ambtown.mp3", xtown, ytown, ztown, true) setSoundMaxDistance(ambtown, 1000) setSoundPanningEnabled (ambtown, false) end end addEvent("onClientPlaySound", true) addEventHandler("onClientPlaySound", resourceRoot, onClientPlaySound) -- server function timeFunction() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute if hours == 07 and minutes == 00 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) end end setTimer(timeFunction, 60000, 0) Share this post Link to post
majqq 88 Posted October 9 13 minutes ago, ShiDiNips said: -- client local locsfortown:~ = { {-86.4970703125, 1120.2216796875, 0}, {-1475.625, 2629.337890625, 0}, {-2445.4267578125, 2388.9111328125, 0} } for i=1 ,#locsfortown:~ do function onClientPlaySound() xtown, ytown, ztown = locsfortown:~[i][1], locsfortown:~[i][2], locsfortown:~[i][3] ambtown = playSound3D("ambtown.mp3", xtown, ytown, ztown, true) setSoundMaxDistance(ambtown, 1000) setSoundPanningEnabled (ambtown, false) end end addEvent("onClientPlaySound", true) addEventHandler("onClientPlaySound", resourceRoot, onClientPlaySound) -- server function timeFunction() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute if hours == 07 and minutes == 00 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) end end setTimer(timeFunction, 60000, 0) My bad, probably found out a issue. -- Serverside function timeFunction() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute -- here if hours == 7 and minutes == 0 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) end end setTimer(timeFunction, 60000, 0) -- 60 000 = 1 minute, 0 = infinite times -- Clientside function onClientPlaySound() local x, y, z = 0, 0, 0 -- change your coordinates here local sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound, 1000) setSoundPanningEnabled(sound, true) end addEvent("onClientPlaySound", true) addEventHandler("onClientPlaySound", resourceRoot, onClientPlaySound) Share this post Link to post
ShiDiNips 0 Posted October 9 3 minutes ago, majqq said: My bad, probably found out a issue. -- Serverside function timeFunction() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute -- here if hours == 7 and minutes == 0 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) end end setTimer(timeFunction, 60000, 0) -- 60 000 = 1 minute, 0 = infinite times -- Clientside function onClientPlaySound() local x, y, z = 0, 0, 0 -- change your coordinates here local sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound, 1000) setSoundPanningEnabled(sound, true) end addEvent("onClientPlaySound", true) addEventHandler("onClientPlaySound", resourceRoot, onClientPlaySound) still doesn't give me anything Share this post Link to post
majqq 88 Posted October 9 7 minutes ago, ShiDiNips said: still doesn't give me anything Are you sure that you changed hour and minute in server-side part? Works for me... -- Serverside function timeFunction() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute -- here if hours == 21 and minutes == 54 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) end end setTimer(timeFunction, 1000, 0) -- 60 000 = 1 minute, 0 = infinite times -- Clientside function onClientPlaySound() --[[local x, y, z = 0, 0, 0 -- change your coordinates here local sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound, 1000) setSoundPanningEnabled(sound, true)]] outputDebugString("test") end addEvent("onClientPlaySound", true) addEventHandler("onClientPlaySound", resourceRoot, onClientPlaySound) Share this post Link to post
ShiDiNips 0 Posted October 10 9 hours ago, majqq said: Are you sure that you changed hour and minute in server-side part? Works for me... -- Serverside function timeFunction() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute -- here if hours == 21 and minutes == 54 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) end end setTimer(timeFunction, 1000, 0) -- 60 000 = 1 minute, 0 = infinite times -- Clientside function onClientPlaySound() --[[local x, y, z = 0, 0, 0 -- change your coordinates here local sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound, 1000) setSoundPanningEnabled(sound, true)]] outputDebugString("test") end addEvent("onClientPlaySound", true) addEventHandler("onClientPlaySound", resourceRoot, onClientPlaySound) Yes, it seems like it dont work for me Share this post Link to post
ShiDiNips 0 Posted October 10 10 hours ago, majqq said: Are you sure that you changed hour and minute in server-side part? Works for me... -- Serverside function timeFunction() local realtime = getRealTime() local hours = realtime.hour local minutes = realtime.minute -- here if hours == 21 and minutes == 54 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) end end setTimer(timeFunction, 1000, 0) -- 60 000 = 1 minute, 0 = infinite times -- Clientside function onClientPlaySound() --[[local x, y, z = 0, 0, 0 -- change your coordinates here local sound = playSound3D("daytime.mp3", x, y, z, true) setSoundMaxDistance(sound, 1000) setSoundPanningEnabled(sound, true)]] outputDebugString("test") end addEvent("onClientPlaySound", true) addEventHandler("onClientPlaySound", resourceRoot, onClientPlaySound) Because I have this... local locationstown = { {-90.736328125, 1122.5888671875, 0}, {-1475.625, 2629.337890625, 0}, {-2445.4267578125, 2388.9111328125, 0} } for i=1 ,#locationstown do local xtown, ytown, ztown = locationstown[i][1], locationstown[i][2], locationstown[i][3] function ambtowntiming() local realtime = getRealTme() local hours = realtime.hour local minutes = realtime.minute if hours == 06 and minutes == 30 then ambtown = playSound3D("ambtown.mp3", xtown, ytown, ztown, true) setSoundMaxDistance(ambtown, 1000) setSoundPanningEnabled (ambtown, false) stopSound(ambtownnight) elseif hours == 22 and minutes == 30 then ambtownnight = playSound3D("ambtownnight.mp3", xtown, ytown, ztown, true) setSoundMaxDistance(ambtownnight, 1000) setSoundPanningEnabled (ambtownnight, false) stopSound(ambtown) end end end -- everything in client function sorry for double quote Share this post Link to post
majqq 88 Posted October 10 Change if hours == 06 and minutes == 30 then to if hours == 6 and minutes == 30 then Besides, loop is only executed only when client resource start. Use setTimer, but i just want to say, that it will take time from player's PC. This function gets the server or client (if used client sided it returns time as set on client's computer) real time and returns it in a table. If you want to get the in-game time (shown on GTA's clock) use getTime. Share this post Link to post
ShiDiNips 0 Posted October 10 1 hour ago, majqq said: Change if hours == 06 and minutes == 30 then to if hours == 6 and minutes == 30 then Besides, loop is only executed only when client resource start. Use setTimer, but i just want to say, that it will take time from player's PC. This function gets the server or client (if used client sided it returns time as set on client's computer) real time and returns it in a table. If you want to get the in-game time (shown on GTA's clock) use getTime. If I do setTimer(theFunction, 60000,0) it will repeat the sound every 1 minute and that will be annoying tho Share this post Link to post
majqq 88 Posted October 10 3 hours ago, ShiDiNips said: If I do setTimer(theFunction, 60000,0) it will repeat the sound every 1 minute and that will be annoying tho Change loop for sound to false, and save sound in variable and check it later? Share this post Link to post
ShiDiNips 0 Posted October 10 40 minutes ago, majqq said: Change loop for sound to false, and save sound in variable and check it later? how about this? -- server function timeFunction(sourcePlayer) local timehour = getTime() if timehour == 6 or timehour == 7 or timehour == 8 or timehour == 9 or timehour == 10 or timehour == 11 or timehour == 12 or timehour == 13 or timehour == 14 or timehour == 15 or timehour == 16 or timehour == 17 or timehour == 18 or timehour == 19 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) triggerClientEvent(getRootElement(), "onClientPlaySoundAmb", resourceRoot) end end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), timeFunction) function timeFunctionNight(sourcePlayer) local timehour, timeminute = getTime() if timehour == 20 or timehour == 21 or timehour == 22 or timehour == 23 or timehour == 0 or timehour == 1 or timehour == 2 or timehour == 3 or timehour == 4 or timehour == 5 then triggerClientEvent(getRootElement(), "onClientPlaySoundNight", resourceRoot) triggerClientEvent(getRootElement(), "onClientPlaySoundAmbNight", resourceRoot) end end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), timeFunctionNight) but on debugscript, it says "ERROR: Server triggered clientside event onClientPlaySound, but event is not added clientside" it's added in the client file Share this post Link to post
MrTasty 129 Posted October 10 (edited) -- server function timeFunction(sourcePlayer) local timehour = getTime() if timehour == 6 or timehour == 7 or timehour == 8 or timehour == 9 or timehour == 10 or timehour == 11 or timehour == 12 or timehour == 13 or timehour == 14 or timehour == 15 or timehour == 16 or timehour == 17 or timehour == 18 or timehour == 19 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) triggerClientEvent(getRootElement(), "onClientPlaySoundAmb", resourceRoot) end end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), timeFunction) function timeFunctionNight(sourcePlayer) local timehour, timeminute = getTime() if timehour == 20 or timehour == 21 or timehour == 22 or timehour == 23 or timehour == 0 or timehour == 1 or timehour == 2 or timehour == 3 or timehour == 4 or timehour == 5 then triggerClientEvent(getRootElement(), "onClientPlaySoundNight", resourceRoot) triggerClientEvent(getRootElement(), "onClientPlaySoundAmbNight", resourceRoot) end end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), timeFunctionNight) This won't work. You need to put it on a timer otherwise you're checking the time only at the time the resource starts. What you want to do is check repeatedly, for example, every minute, if the time is a full hour (that is, minutes == 0) and if so, play the sound, otherwise ignore the call. local lastTrigger -- at what hour was the sound last played (to prevent replays in same hour) -- server function timeFunction() local timehour, timeminute = getTime() if lastTrigger == timehour then return -- abort here if we already played the sound for this hour (in case the timer manages to execute twice within the same hour) end if (timehour >= 6 and timehour <= 19) and timeminute == 0 then -- if it's between 6am and 7pm and it's a full hour triggerClientEvent(root, "onClientPlaySound", resourceRoot) triggerClientEvent(root, "onClientPlaySoundAmb", resourceRoot) lastTrigger == timehour -- mark the hour at which it was last triggered elseif (timehour >= 20 or timehour <= 5) and timeminute == 0 then -- if it's 8pm or after, or 5am or before and it's a full hour triggerClientEvent(root, "onClientPlaySoundNight", resourceRoot) triggerClientEvent(root, "onClientPlaySoundAmbNight", resourceRoot) lastTrigger == timehour -- mark the hour at which it was last triggered end end function init() setTimer(timeFunction, 1000, 0) -- keep checking the time every second end addEventHandler("onResourceStart", resourceRoot, init) Edited October 10 by MrTasty Share this post Link to post
ShiDiNips 0 Posted October 12 On 10/10/2019 at 22:25, MrTasty said: -- server function timeFunction(sourcePlayer) local timehour = getTime() if timehour == 6 or timehour == 7 or timehour == 8 or timehour == 9 or timehour == 10 or timehour == 11 or timehour == 12 or timehour == 13 or timehour == 14 or timehour == 15 or timehour == 16 or timehour == 17 or timehour == 18 or timehour == 19 then triggerClientEvent(getRootElement(), "onClientPlaySound", resourceRoot) triggerClientEvent(getRootElement(), "onClientPlaySoundAmb", resourceRoot) end end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), timeFunction) function timeFunctionNight(sourcePlayer) local timehour, timeminute = getTime() if timehour == 20 or timehour == 21 or timehour == 22 or timehour == 23 or timehour == 0 or timehour == 1 or timehour == 2 or timehour == 3 or timehour == 4 or timehour == 5 then triggerClientEvent(getRootElement(), "onClientPlaySoundNight", resourceRoot) triggerClientEvent(getRootElement(), "onClientPlaySoundAmbNight", resourceRoot) end end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), timeFunctionNight) This won't work. You need to put it on a timer otherwise you're checking the time only at the time the resource starts. What you want to do is check repeatedly, for example, every minute, if the time is a full hour (that is, minutes == 0) and if so, play the sound, otherwise ignore the call. local lastTrigger -- at what hour was the sound last played (to prevent replays in same hour) -- server function timeFunction() local timehour, timeminute = getTime() if lastTrigger == timehour then return -- abort here if we already played the sound for this hour (in case the timer manages to execute twice within the same hour) end if (timehour >= 6 and timehour <= 19) and timeminute == 0 then -- if it's between 6am and 7pm and it's a full hour triggerClientEvent(root, "onClientPlaySound", resourceRoot) triggerClientEvent(root, "onClientPlaySoundAmb", resourceRoot) lastTrigger == timehour -- mark the hour at which it was last triggered elseif (timehour >= 20 or timehour <= 5) and timeminute == 0 then -- if it's 8pm or after, or 5am or before and it's a full hour triggerClientEvent(root, "onClientPlaySoundNight", resourceRoot) triggerClientEvent(root, "onClientPlaySoundAmbNight", resourceRoot) lastTrigger == timehour -- mark the hour at which it was last triggered end end function init() setTimer(timeFunction, 1000, 0) -- keep checking the time every second end addEventHandler("onResourceStart", resourceRoot, init) "lastTrigger" wasn't recognized that's why It keeps calling and spamming the "onClientPlaySound" every minute which makes the server and the players lag Share this post Link to post
MrTasty 129 Posted October 12 local lastTrigger -- at what hour was the sound last played (to prevent replays in same hour) -- server function timeFunction() local timehour, timeminute = getTime() if lastTrigger == timehour then return -- abort here if we already played the sound for this hour (in case the timer manages to execute twice within the same hour) end if (timehour >= 6 and timehour <= 19) and timeminute == 0 then -- if it's between 6am and 7pm and it's a full hour triggerClientEvent(root, "onClientPlaySound", resourceRoot) triggerClientEvent(root, "onClientPlaySoundAmb", resourceRoot) lastTrigger = timehour -- mark the hour at which it was last triggered elseif (timehour >= 20 or timehour <= 5) and timeminute == 0 then -- if it's 8pm or after, or 5am or before and it's a full hour triggerClientEvent(root, "onClientPlaySoundNight", resourceRoot) triggerClientEvent(root, "onClientPlaySoundAmbNight", resourceRoot) lastTrigger = timehour -- mark the hour at which it was last triggered end end function init() setTimer(timeFunction, 1000, 0) -- keep checking the time every second end addEventHandler("onResourceStart", resourceRoot, init) (slightly corrected, accidentally used equality comparison rather than assignment in lastTrigger and timehour.) This code works for me, triggering these events every minute (an ingame hour). If you want it to work on real hours, you need to use local lastTrigger -- at what hour was the sound last played (to prevent replays in same hour) -- server function timeFunction() local realtime = getRealTime() local timehour, timeminute = realtime.hour, realtime.minute if lastTrigger == timehour then return -- abort here if we already played the sound for this hour (in case the timer manages to execute twice within the same hour) end if (timehour >= 6 and timehour <= 19) and timeminute == 0 then -- if it's between 6am and 7pm and it's a full hour triggerClientEvent(root, "onClientPlaySound", resourceRoot) triggerClientEvent(root, "onClientPlaySoundAmb", resourceRoot) lastTrigger = timehour -- mark the hour at which it was last triggered elseif (timehour >= 20 or timehour <= 5) and timeminute == 0 then -- if it's 8pm or after, or 5am or before and it's a full hour triggerClientEvent(root, "onClientPlaySoundNight", resourceRoot) triggerClientEvent(root, "onClientPlaySoundAmbNight", resourceRoot) lastTrigger = timehour -- mark the hour at which it was last triggered end end function init() setTimer(timeFunction, 60000, 0) -- keep checking the time every minute end addEventHandler("onResourceStart", resourceRoot, init) Share this post Link to post