How do I get from a list in my config a random one and use it as string?
Like the notifier plugin which randomly chooses a broadcasted message out of the config.
Solved Getting random from config as string (LUA)
Discussion in 'Rust Development' started by LaserHydra, Apr 8, 2015.
-
Make enumed list of messages in config, read it. And then randomize nums for calling diff messages?
-
Well not messages, that was an example but yes. I mean that I got a list in my config file, and I want to get a random one out of that list.
[DOUBLEPOST=1428577171][/DOUBLEPOST]
Code:{ "Messages": "This is a Oxide Server", "I like turtles", "Apples are red", "Random messages?" }
-
Still, you have array of strings, just get his length and randomize ints from 0 to Length - 1
and call it:
local randomizedInt = math.random(0, messages.Length -1)
message = messages[randomizedInt] -
that does not work for me....
Code:function PLUGIN:LoadDefaultConfig() self.Config.Messages = self.Config.Messages or "Message1", "Message2", "Message3" endfunction PLUGIN:cmdBroadcast() local randomizedInt = math.random(0,Messages.Length -1) Message = Messages[randomizedInt] rust.RunServerCommand("say " .. Message) end
-
Code:self.Config.Messages = self.Config.Messages or { "Message 1", "Message 2", "Message 3" }
You also don't need to run the console command say, you can use the rust.BroadcastChat helper to broadcast a message.
Code:function PLUGIN:cmdBroadcast() local message = self.Config.Messages[math.random(1, #self.Config.Messages)] rust.BroadcastChat(message) end
-
Code:
Failed to call hook 'cmdRandomBcast' on plugin 'Random Broadcaster' File: randomitem.lua Line: 27 attempt to concatenate local 'message' (a nil value)
-
-
-
What if i wanted to give random "items" with the same way ?
One more question how to give it to random player...
Code:function PLUGIN:LoadDefaultConfig() self.Config.RandomItems = self.Config.RandomItems or {"Pick Axe","Wood","Brain"}endfunction PLUGIN:cmdRandom() local items = self.Config.RandomItems[math.random(1,#self.Config.RandomItems)] rust.RunServerCommand(inventory.giveid {player} items) end
-