So i was thinking about box opening systems like Murder games etc… but when i try to remake them or figue out how they work i hit a wall and cant really understand what they do to acchive that box/crate opening…
you do not know how they choose a random item or the animations ?
what you mean is rng
there are many ways to make rngs systems one of them is weighted rngs where every item will be given a chance
local knifes = {
{name = "BadKnife", weight = 50}, -- weight is the chance
{name = "DecentKnife", weight = 30},
{name = "ProKnife", weight = 15},
{name = "MythicalKnife", weight = 5}
}
-- the lower the weight the higher the chance
local totalWeight = 0 -- doesnot have to be 100
-- calculating totalWeight using a for loop
for index, knife in knifes do
totalWeight += knife.weight
end
-- A function that rolls a random knife
local function roll()
local randomNumber = math.random() * totalWeight -- multiplying by math.random allows for decimal numbers
local currentWeight = randomNumber -- setting currentWeight to random number
for index, knife in knifes do
currentWeight -= knife.weight
if currentWeight <= 0 then
return knife
end
end
end
local a = {
["BadKnife"] = 0,
["DecentKnife"] = 0,
["ProKnife"] = 0,
["MythicalKnife"] = 0,
}
for i=0,100 do
a[roll().name] += 1 -- a test to test the luck system
end
print(a)
how this works
first you set a totalWeight variable #doesnot have to be 100
and then in the roll function
we set a variable called currentWeight for example and we set it to a random value from 0 to total weight
we then loop through all the knifes in the table and subtract the currentWeight from the knife weight if its less than 0 then we return it
for instance if currentWeight was equal to 40
the loop will first subtract 40 from 50(the bad knife weight) and result in -10 and -10 is < 0 so we return it
you can think of it that way
BadKnife chance is from [0,50]
decent knife chance is ]50,80]
ProKnife chance is from ]80,95]
etc…
what about the opening animation part its the most complicated part for me
clone the box from the client , put in front the player camera then shake it “look for the direction that you wanna shake and increase it and decrease it randomly”
i was experimenting with script but i noticed the for loop goes in index order even though it is not ipairs why does it behave like that?
when you assign a value of the table without giving it a key its key will be automaticlly set to 1 +
example–
tbl = {
{} -- key is 1
{} -- key is 2
{} -- key is 3
etc..
}