You can write your topic however you want, but you need to answer these questions:
I want to make an randomizer for cases with like 10% 10% 60% 20%
2 I cant really find anything that fits very well in my code
game.ReplicatedStorage.Events.BuyingCase.OnClientEvent:Connect(function(Bought)
if Bought == true then
for i, C in pairs(Cases.Items:GetChildren()) do
local Percentage = C.Percent.Value
end
end
end)
I don’t think it’s possible to generate random percentages from a minimum value to a maximum one.
I dont think it matters either if you would generate a random percentage because you can easily achieve that by just generating a random number and putting the percentage mark after it.
I suggest you do this:
local Random = math.random(0.0, 50.0)
local Percentage = Random.."%"
3 I need to have an randomizer that will choose an case based on its percentage and then open it
function openCase()
if haveKey then
for i, C in pairs(Cases.Items:GetChildren()) do
local Percentage = C.Percent.Value
end
--[[
CODE HERE
]]
game.ReplicatedStorage.Events.BuyingCase:FireServer(false)
else
Infos.Text = "You need to buy a case key to open a case!"
wait(5)
Infos.Text = "Infos"
end
end
4 The range would be 0-100, I dont know how to make the range bigger so I can add the percentages needed.
function openCase()
if haveKey then
for i, C in pairs(Cases.Items:GetChildren()) do
local Percentage = C.Percent.Value
end
--[[
CODE HERE
]]
game.ReplicatedStorage.Events.BuyingCase:FireServer(false)
else
Infos.Text = "You need to buy a case key to open a case!"
wait(5)
Infos.Text = "Infos"
end
end
5 I am going to add more cases later on so I need to be able to add more percentages
function openCase()
if haveKey then
for i, C in pairs(Cases.Items:GetChildren()) do
Don’t approach it as using percentages, that’s just a visual way to show the user. Assign each item in the case with a weight, higher weights for more common items. Add up all of the weights of all the items you want in the case. Percentages came be made by diving the weight of each item by the total weight.
Start randomly picking items within that case and subtracting it from the total, if the total goes below zero you pick that item. Rarer items are less likely to trigger the threshold to be picked.
local randomNumber = Random.new()
local weightTable = {
["Wood Sword"] = 40,
["Stone Sword"] = 35,
["Diamond Sword"] = 15,
["Plasma Sword"] = 10
}
local function caseResult(weightTable)
local selectedItem
local totalWeight = 0
for _,value in pairs(weightTable) do
totalWeight += value
end
local ticket = randomNumber:NextInteger(1,totalWeight)
for item,value in pairs(weightTable) do
ticket -= value
print(ticket)
if ticket <= 0 then
selectedItem = item
break
end
end
return selectedItem
end