{"id":"minefranck/rngservice","name":"rngservice","scope":"minefranck","platform":"roblox","description":"Mirrored from the Wally registry.","version":"0.1.3","latest":"0.1.3","versions":["0.1.0","0.1.1","0.1.2","0.1.3"],"license":"MIT","licenseRating":"safe","licenseCaveats":["The package archive does not include its license text; the license is declared in its manifest metadata."],"licenseVerified":false,"dependencies":{},"integrity":"b4196ac02073657dbfa2e3837bd2f23c5414c6f78b549c374dd4480ed2dbc8b1","likes":0,"downloads":0,"install":"forest install minefranck/rngservice","url":"https://forest.dev/p/roblox/minefranck/rngservice","files":"https://api.forest.dev/ai/package/roblox/minefranck/rngservice/files","readme":"# RNGService\r\nRNGService allows you to easily create your own RNG systems for Roblox luau\r\n\r\n## Wally Install\r\n```\r\nrngservice = \"minefranck/rngservice@0.1.2\"\r\n```\r\n\r\n## Utils\r\n#### GenerateRNG\r\n```lua\r\nlocal random = RNGServiceUtils:GenerateRNG()\r\nprint(random:NextInteger(1, 5))\r\n-- prints a random number from 1 to 5\r\n```\r\nSame use as math.random(), but prevents predictable rng.\r\nNot pratical if only being used for basic stuff. Use if it directly affects the players data/gameplay.\r\nCredits: Stewiepfing on Youtube https://www.youtube.com/@stewiepfing\r\n\r\n#### GetSum\r\n```lua\r\nlocal rarities = {\r\n  Common = {\r\n    Weight = 100,\r\n    Tier = 0\r\n  },\r\n  Rare = {\r\n    Weight = 50,\r\n    Tier = 1\r\n  }\r\n}\r\n```\r\nReturns the sum of all weights inside the rarities table\r\n\r\n## Service\r\n### Initialization\r\n```lua\r\nRNGServiceShared:Init(0.5)\r\n-- RNGServiceShared.LuckConstant will be set to 0.5 (default 0.15)\r\n```\r\nIf the service is not initialized, it still works, but LuckConstant will be set to 0.15 (recommended value).\r\nAs the service is shared, u have to init on both server and client if you want to change LuckConstant.\r\n\r\n### Methods\r\n#### GetRandomKey\r\n```lua\r\nlocal rarities = {\r\n  Common = {\r\n    Weight = 75,\r\n    Tier = 0\r\n  },\r\n  Rare = {\r\n    Weight = 25,\r\n    Tier = 1\r\n  }\r\n}\r\n\r\nlocal random = RNGServiceShared:GetRandomKey(rarities, luck)\r\n-- returns a random key in weights table\r\nprint(random)\r\n-- 75% chance of printing \"Common\" and 25% of printing \"Rare\" if luck is <=1 or nil\r\n```\r\nReturns a random key from the given table\r\n\r\n#### GetRandomFromArray\r\n```lua\r\nlocal array = {\"apple\", \"orange\", \"banana\"}\r\nlocal random = RNGServiceShared:GetRandomFromArray(array)\r\nprint(random)\r\n-- prints a random item from the array. in this case, 33.3% chance of printing any\r\n```\r\nReturns a random item from the given array, with each item having the same chance of being picked\r\n\r\n#### CallbackOnChance\r\n```lua\r\nRNGServiceShared:CallbackOnChance(5, function()\r\n  print(\"Hi\")\r\nend)\r\n-- 1 in 5 chance of executing print(\"Hi\")\r\n\r\nlocal bool = RNGServiceShared:CallbackOnChance(10, function()\r\n  return true\r\nend)\r\nprint(bool)\r\n-- bool has a 1 in 10 chance of being true, else its just nil\r\n```\r\nHas a 1 in x chance of executing the given callback. If callback returns any value(s), CallbackOnChance returns them.\r\n\r\n#### GetPercentage\r\n```lua\r\nlocal rarities = {\r\n  Common = {\r\n    Weight = 3,\r\n    Tier = 0\r\n  },\r\n  Rare = {\r\n    Weight = 1,\r\n    Tier = 1\r\n  }\r\n}\r\n\r\nlocal percentages = RNGServiceShared:GetPercentage(rarities, luck)\r\nprint(percentages)\r\n--[[\r\nprints if luck <= 1 or nil: {\r\n  Common = 75,\r\n  Rare = 25\r\n}\r\n]]\r\n```\r\nReturns a table containing all in percentage chance for the given weights table\r\n\r\n#### ApplyLuck\r\n```lua\r\nlocal rarities = {\r\n  Common = {\r\n    Weight = 100,\r\n    Tier = 0\r\n  },\r\n  Rare = {\r\n    Weight = 50,\r\n    Tier = 1\r\n  }\r\n}\r\nlocal luck = 10\r\n\r\nlocal newRarities = RNGServiceShared:ApplyLuck(rarities, luck)\r\nprint(newRarities)\r\n--[[\r\nprints: {\r\n  Common = {\r\n    Weight = 100,\r\n    Tier = 0\r\n  },\r\n  Rare = {\r\n    Weight = 158,\r\n    Tier = 1\r\n  }\r\n}\r\n]]\r\n```\r\nReturns a new weights table with the actual weights updated according to the luck inputed\r\n\r\n## Info\r\n\r\n### Weight\r\nWeight is a number that indicates how common something should be. The higher the weight, the more common it will be.\r\n\r\n### Tiers\r\nTier is a number that directly affects how much its chances are boosted for a certain luck value.\r\nExample: If Weight = 50 and Tier = 0, at luck 2, Weight = 50 still (Tier 0 never changes the weight). If Weight = 50 and Tier = 1, at luck 2, Weight = 55 (if constant = 0.15)\r\n\r\n## Credits\r\nStewiepfing for designing GenerateRNG(). https://www.youtube.com/@stewiepfing\r\n","readmeTruncated":false}