I wanted to create a Script, where the Player who is writing "event" is going to See a number from 1-5.
[code]random = {}
random[1] = 1
random[2] = 2
random[3] = 3
function event()
print(math.random(random)
end[/code]
Now I tried only the random number function.
But there is not showing a number?
What did I wrong
[editline]11th January 2017[/editline]
Ok I found a misstake after random) I forgot")" but that didnt changes anything
Are you even calling the function "event"?
[code]random = {}
random[1] = 1
random[2] = 2
random[3] = 3
function event()
print(math.random(random))
end
print(event)[/code]
So? xD sorry im new at lua ;D
[QUOTE=xStylo27;51654894][code]random = {}
random[1] = 1
random[2] = 2
random[3] = 3
function event()
print(math.random(random))
end
event()[/code]
[/QUOTE]
input:8: bad argument #1 to 'random' (number expected, got table)
This is showing on lua demo
Its only showing "3" :(
What does 1, 3 do? Counting all tables from 1 to 3? :D
It's supposed to pick a random number between 1 and 3. For some reason it's only picking 3. Not quite sure why.
ok, so I dont need to do the random = {} thing?
[QUOTE=xStylo27;51654955]ok, so I dont need to do the random = {} thing?[/QUOTE]
You do need [QUOTE]random = {}[/QUOTE], that's what defines the table, without that, the 3 lines after it are useless.
The "random = {} thing" is you table from what you are picking a random value with
[lua]
random[math.random(1,3)]
[/lua]
Edit:
Well, posted too late. :c
ok, thanks!
I will try it with more numbers :D
So new question:
Is there a way that when a Player is picking the Right number is getting Pointshop points?
This code is returning only the number 3...
[url]https://goo.gl/ANUYyS[/url]
I don't know if i'm just being dumb or what, but it should generate a random number between 1 and 3...
:(
Idk if i am dumb too, but i think the math.random() function doesnt work properly
[url]https://goo.gl/32spFz[/url]
For me this is only printing 10.
Bist du nicht von Suchtbunker? xD
[editline]11th January 2017[/editline]
:( I wanted to create smth like an raffle
[editline]11th January 2017[/editline]
Everyone is typing a number between 1-25 and everyone who has the right number gets 500 Pointshop Points
The issue is with how the random number generator is seeded. If you use random multiple times in the same code you will get different numbers, but if you run it once, stop executing, run again etc. you will always get the same number the first time.
[QUOTE=Shp0;51655025]The issue is with how the random number generator is seeded. If you use random multiple times in the same code you will get different numbers, but if you run it once, stop executing, run again etc. you will always get the same number the first time.[/QUOTE]
Completely forgot that.. xD
So I can´t create a Raffle?
If you run the code in-game the math.random lib works as it should.
Ok I will try it!
[editline]11th January 2017[/editline]
But one thing... where should I put it in?
[QUOTE=xStylo27;51655030]So I can´t create a Raffle?[/QUOTE]
Your code will work once it's a process continually executing. If you wanted to see a different result with your first code you posted you would have to randomly seed the random number generator, such as by using math.randomseed here [URL="http://lua-users.org/wiki/MathLibraryTutorial"]http://lua-users.org/wiki/MathLibraryTutorial[/URL].
[code]function event()
if ply:Say("!event") then
PrintMessage( HUDPRINTTALK, "Annddd"..ply:Nick().."got"..math.random(1,25).."!")
end
end[/code] would this work?
[editline]11th January 2017[/editline]
I forgot calling the function sry
No, you need to run the function on a "PlayerSay" hook.
[lua]
hook.Add("PlayerSay","EventChatCMD",function(ply,text)
if text == "!event" then
PrintMessage( HUD_PRINTTALK, "Annddd "..ply:Nick().." got "..math.random(1,25).."!")
end
end)
[/lua]
Okay thanks I will try it!
How is the command called to check that the players picked number is same as the random num?
You mean if a player types something like "!event 12" and if the random number is 12 too?
Yes
For that you need to "explode" the text that the player types.
[lua]
hook.Add("PlayerSay","EventChatCMD",function(ply,text)
local tbl = string.Explode(" ",text)
if tbl[1] == "!event" and tbl[2] then
local randnum = math.random(1,25)
local num = tonumber(tbl[2])
PrintMessage( HUD_PRINTTALK, "Annddd "..ply:Nick().." got "..randnum.."!")
if randnum == num then
// Your code if the number is the same
else
// Your code if the number is not the same
end
end
end)
[/lua]
Ouh man lua is hard xD
Where did you learned all of these cmds?
[editline]11th January 2017[/editline]
And THANKS! But where do I have to put my .lua file?
The [URL="http://wiki.garrysmod.com/"]wiki[/URL] is a very useful place.
You need to put the file where the server will run it.
You can put it in lua/autorun/server/yourfilename.lua
Sorry, you need to Log In to post a reply to this thread.