math.random(1,10) generating multiplae values at once
31 replies, posted
So, for a addon im creating right now i need a random number between 1 and 10, and then a specific event will happen depending on what number it is at the moment it checks. Problem: Sometimes NO event happens because NO number gets created, or maybe even only one number just how its supposed to be, OR at most times, several numbers! like 5 and 7, 6 and 9, 10 and 10, and so on. What? Isnt math.random supposed to return ONE number and not a random amount of random numbers?
No code?
hook.Add("PostGamemodeLoaded", "AfterGamemodeLoaded", function()
local Twice = 0
hook.Add("Think", "Kato", function()
local Player = LocalPlayer()
local health = Player:Health()
while Player:Alive() and health <= 0 and Twice == 0 do
local Random = math.random(1,10)
print(Random)
if Random == 1 then
surface.PlaySound( "Player_dead/Terminate_angry1.mp3" )
elseif Random == 2 then
surface.PlaySound( "Player_dead/Terminate_angry2.mp3" )
elseif Random == 3 then
surface.PlaySound( "Player_dead/Terminate_angry3.mp3" )
elseif Random == 4 then
surface.PlaySound( "Player_dead/Terminate_angry4.mp3" )
elseif Random == 5 then
surface.PlaySound( "Player_dead/Terminate1.mp3" )
elseif Random == 6 then
surface.PlaySound( "Player_dead/Terminate2.mp3" )
elseif Random == 7 then
surface.PlaySound( "Player_dead/Terminate3.mp3" )
elseif Random == 8 then
surface.PlaySound( "Player_dead/Terminate4.mp3" )
elseif Random == 9 then
surface.PlaySound( "Player_dead/Terminate5.mp3" )
elseif Random == 10 then
surface.PlaySound( "Player_dead/Terminate6.mp3" )
end
break
end
end)
end)
dont really know why you need the code..since the problem is literally just, math.random returns the same number, and sometimes several
You don't set your Twice variable (in that while loop) to 1 (I think you supposed to), so it will spam you with different sounds. Why won't you use a server-side hook like GM/DoPlayerDeath with a net message?
I realized the Twice variable is useless, so i removed it now. Also im not using a server side hook because i dont know netrworking that much. It confuses me..but i dont really need to, since i can start every sound clientside, when the clients health is 0 and hes dead. No need to have the server check that, AND send it over..everything would be perfectly fine, if math.random would just return one freaking number at once, and not several nuzmbers sometimes, or the same the whole time, or sometimes even no number at all!
math.randomseed(os.time())
local random_number = math.random(1, 10)
btw, do you see a problem here?
if Random == 1 then
surface.PlaySound( "Player_dead/Terminate_angry1.mp3" )
elseif Random == 2 then
surface.PlaySound( "Player_dead/Terminate_angry2.mp3" )
elseif Random == 3 then
surface.PlaySound( "Player_dead/Terminate_angry3.mp3" )
elseif Random == 4 then
surface.PlaySound( "Player_dead/Terminate_angry4.mp3" )
elseif Random == 5 then
surface.PlaySound( "Player_dead/Terminate1.mp3" )
elseif Random == 6 then
surface.PlaySound( "Player_dead/Terminate2.mp3" )
elseif Random == 7 then
surface.PlaySound( "Player_dead/Terminate3.mp3" )
elseif Random == 8 then
surface.PlaySound( "Player_dead/Terminate4.mp3" )
elseif Random == 9 then
surface.PlaySound( "Player_dead/Terminate5.mp3" )
elseif Random == 10 then
surface.PlaySound( "Player_dead/Terminate6.mp3" )
end
math.randomseed only makes sure the random number is always something else, but that doesnt help me..it still returns several numbers at once sometimes, making several sounds play at once. Also no i dont see whats wrong with my check.
https://pastebin.com/95vcxBVH
There's so much I just am not understanding...
Why do you have your Think hook in another Hook?
Why are you using a while loop in a hook that runs every tick?
Why aren't you using a table for all of your sounds?
So, the problem isn't math.random at all, it's all this wrong stuff you're doing everywhere else...
i dont need networking...literally everything works fine clientside, JUST math.random sometimes returns nothing, or 2 numbers at once. Which isnt really possible because it only checks for math.random once, and math.random is not a table..so how the f can it be more than 1 number
It's in my code snippet
yea. didnt helped at all. Still plays 2 sounds at once or even 3
attempt to call a nil value (global 'GetLogicalPointOfView')
wow thanks for that helpfull comment...that sure solves my problem and my lacking knowledge why it returns several numbers at once.. -.-
It's not, it means that your hook or whatever is being executed more than once. Try to completely remove your code from addons and place my code in lua/autorun
It doesn't return multiple values, it's your code running more than once. The reason it's doing this is because Player/Alive is returning true even when your health is at zero, and it's doing this for multiple frames.
The reason for doing this is probably because as in my wording above, GM/Think runs every frame on the client, not every tick. Because the engine doesn't update the death state every frame your game is probably running fast enough such that it catches it before it updates that state. Likewise because these actions aren't synced it could also see a state such that it doesn't run at all.
If you insist on doing this client side only then I'd suggest using a GM/Tick hook and instead checking when the state of http://wiki.garrysmod.com/page/Player/Alive changes, you can do this by storing the last value in a variable. I'd suggest just doing this server side and networking it.
but your code was serverside only, was it? also the only reason it could execute twice or so would be because its all in a think hook, but it has to be in a think hook, otherwise it checks if the player is dead once, when you spawn or before, and then never again..or is there some other way to constantly check? think hook is the only way i know
It's not serverside only, its both - serverside and clientside (it has a SERVER/CLIENT statement). Please, read Net Library Usage and Net Library Example, its really simple to use and understand.
so basically i put your script in server AND client folder so it gets executed on both sides?
Files from lua/autorun are executed on both client and server, files from lua/autorun/client and lua/autorun/server are only on their side.
As I said in my post, don't check the health, only check the state of the player being alive, and only run when that state changes. Or continue doing as @LoweChaser recommend, it's better.
ah well thats handy..learned something new today..also i know net working, i kinda understand it, tryd it out, worked...and never figured it out how to use it on my addon later on. i tryd your script, this works now, so i take a closer look at it tomorrow..i will probably still rage at the end because my addons has sevral events, not only die. also ammo empty etc. I probably will need networking for all that too..
You don't understand the functions you're trying to use.
From surface.PlaySound:
"Play a sound file directly on the client (such as UI sounds, etc)."
yes i do know how surface
playsound works.
You seem so damn lost here, what is your issue with networking?
try putting this into the serverside code
local sounds = {
"Player_dead/Terminate_angry1.mp3",
"Player_dead/Terminate_angry2.mp3",
"Player_dead/Terminate_angry3.mp3",
"Player_dead/Terminate_angry4.mp3",
"Player_dead/Terminate1.mp3",
"Player_dead/Terminate2.mp3",
"Player_dead/Terminate3.mp3",
"Player_dead/Terminate4.mp3",
"Player_dead/Terminate5.mp3",
"Player_dead/Terminate6.mp3",
}
hook.Add("PlayerDeath","Deathsound",function(ply,weapon,killer)--
if ply and ply:IsValid() then
local soundName=sounds[math.random(1,10)]
ply:EmitSound(soundName)
end
end)
hook.Add("PlayerDeathSound","Deathsound",function()
return true--return true on this hook to mute the default hl2 death sound
end)
i just never really learned it. But i have no issue with it
You're full of it ain't ya? there comes a point where people realize they're in denial.
can you stop now?? You wasnt helpfull since the very first post, and you still answer crap
LoweChaser's code is technically more efficient. But in fact each person's code will accomplish different things. Lowe's will only play sound on the player who died's client, and not in 3D space. Joey's code will make all players within the vicinity of the dead player hear the sound, in 3D space.
Lowe's code can be adapted to do what Joey's is and still be more efficient, but Joey's is simpler to read and understand to a newbie.
It's really just a matter of preference for something this inconsequential, anyway. No real reason to flame him for that post in particular, though his attitude previously in this thread was pretty bad.
Sorry, you need to Log In to post a reply to this thread.