What I am trying to make is a case opening system, this is different then what a normal case system would be. I also what the item that the player gets to go into an inventory gui.
Goals:
When player opens case it gives a random rarity from module based on percentage
Picks random item from that rarity
Item goes into players inventory where they can equip it.
When the game starts player gets the item
I want the code to be as organized as possible, I am trying to use a module script to get the rarity from. I’ve also watched multiple videos and look all around the forums, but nothing as seemed to help me.
How I want the case to open:
I don’t want a spin animation or tween to play, I would rather the item just come straight from the case.
I already know how to get it so the player can only buy the case when it they have a certain amount of cash.
There are going to be two kinds of cases, an uncommon one (where you can’t get commons) and a common one. Each case will have rewards such as: death effects, titles, and weapons.
I already know how to code each one of these, but I don’t know how to reward it to the player.
What I don’t know how to do
Pick rarity based off percentage
Store the items into inventory.
Pick random item based off rarity.
How to use this, like should I use remote events and if so how would I store/excute them
Here is the code i’ve came up with, but i’m already running into problems:
Module Script
local m = {}
m.percentages = {
Common = 50,
Uncommon = 25,
Rare = 15,
Epic = 10
}
return m
Local script:
local percentages = require(game.ServerScriptService.Shop.percentages)
function getRandomObject(table)
local rand = math.random(); -- random number between 0 and 1
local pastPercentage = 0; -- bank so we can add percentage up as we go
for i = 1, #table do --loop through all objects
if rand < table[i].Percentage + pastPercentage then -- check if rand is under the percentage
return table[i]; -- if so, that is our random choice
end
pastPercentage = pastPercentage + table[i].Percentage; -- if it isn't, add its percentage to the bank and scan the other
end
end
print(getRandomObject)
I’ve already run into problems too.
Storage:
If you havent noticed, I’m going to need tons of help.
-- Module --
local percentages = {
--Name = {lowerBound,upperBound}
Common = {50,100},
Uncommon = {25,50},
Rare = {10,25},
Epic = {0,10}
}
-- Script --
local percentages = require(game.ServerScriptService.Shop.percentages)
local getRandomItem = math.random(0,100)
local rarity
for item,bounds in pairs(percentages) do
if getRandomItem >= bounds[1] and getRandomItem < bounds[2] then
rarity = item
break
end
end
print(rarity)
You can store the items in a folder on the Replicated Storage. Using PlayerAdded and PlayerRemoving to create/remove the folder with the items, and you can use DataStore to save the items once the Player logs off. Then use a GUI to display the items to the player by printing all the items stored in the folder using ChildAdded/ChildRemoved to update the GUI list (You can display the contents by using getchildren)
RemoteEvents: You should handle the item picking/giving by a server script located inside the ServerScriptService and use a remove event coming from a local script to fire the event. Then the Server Script should check if the player meets the requirements to open the crate (Like coins or keys).
You can give the item to the player by having the item stored inside Server Storage or Replicated Storage in a folder called by the item rarity and use the function Clone() to give the item to the player. If you do it this way implement the following script:
local ItemsFolder = "" --location of the folder
local itemsList = ItemsFolder:FindFirstChild(rarity):GetChildren() -- get the rarity folder
local randomItem = items[math.random(1, #itemsList)] -- pick a random item
randomItem:Clone().Parent = PlayerFolderLocation -- Give the item to the player
If you need more help reply. I have not tested any of these scripts in the studio, so they can contain some minor errors.
That can mean 2 things
1.- The location for the module is not correct
2.- The scripts does not found the module. In that case your FindFirstChild or WaitForChild
yes. but i also changed the normal script up a bit.
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
local percentages = require(game.ServerScriptService.Shop.percentages)
local getRandomItem = math.random(0,100)
local rarity
for item,bounds in pairs(percentages) do
if getRandomItem >= bounds[1] and getRandomItem < bounds[2] then
rarity = item
break
end
end
print(rarity)
end)
Strange, unfortunately Im not at home rn, I will try to help you once I log into the studio. Prob within the next 12h.
I just saw now in your module there is a blank % between 15 and 20% (rare-uncommon)