-- randomsound.lua -- author Belidos -- re-edits by J_C -- -- Changed script name from RandomSound.lua -- to randomsound.lua -- I believe that the script file name has to be lower case -- if you look at all the scripts in scripbank they are all lower case. -- I think I have seen this stated on the forum at some point in the past.. -- script objective -- i'm trying to create a script so that when you get within a certain distance of an object -- it randomly plays one of four sounds in its slots, -- with a delay of 5 seconds in between sounds. local randomsound = 1 local delay = 5000 local timeout = 1 local near_dist = 500 -- I had to add this variable to show when the sound is active local sound_on = 0 function randomsound_init(e) -- you are re-delclaring these variable here -- so these will be new variables local to this init function -- so not used -- you could remove the local declaration -- but as you have already set them to these values at top then not required here -- -- if these had been array variables then you would set them to a value here -- for this [e] entity number as in timeout[e] = 1 but it is not an array here --local timeout = 1 --local randomsound = 1 end function randomsound_main(e) -- trying to create a script so that when you get within a certain distance of an object PlayerDist = GetPlayerDistance(e) if PlayerDist < near_dist then -- we are near to object if sound_on == 0 then -- and the sound is off -- it randomly plays one of four sounds in its slots, randomsound = math.random(1,4) PlaySound(e,randomsound) sound_on = 1 -- with a delay of 5 seconds in between sounds. -- first reset the timer so we can check later for time delay StartTimer(e) elseif sound_on == 1 then -- sound is playing - has our 5 seconds elapsed if GetTimer(e) > delay then -- times up -- reset sound for new sound sound_on = 0 end end else -- we are not near to object now so turn off sound if on if sound_on == 1 then -- sound is active StopSound(e, randomsound) -- reset for new sound sound_on = 0 end end end --original code function randomsound0_main(e) PlayerDist = GetPlayerDistance(e) if PlayerDist < 500 and GetTimer(e) > delay then randomsound = math.random(1,4) if timeout == 0 then if randomsound == 1 then PlaySound(e,1) timeout =1 end if randomsound == 2 then PlaySound(e,2) timeout =1 end if randomsound == 3 then PlaySound(e,3) timeout =1 end if randomsound == 4 then PlaySound(e,4) timeout =1 end end else if timeout == 1 then StartTimer(e) timeout = 0 end end end