Hello, does anyone know how to make a spinning crate system? I’ve done the random chooser so I just need the spinning system.
There’s undoubtedly people here that know how to accomplish this.
Remember: Scripting Support is not a venue to ask for code. If I’m misinterpreting your thread and you’re asking for guidance or directions on where to look on creating a “spin system”, I apologise. In this manner though, your thread is fairly vague and more details would be helpful.
There are various tutorials around on the net (YouTube specifically) regarding loot crate creation and I’m sure that one of those tutorials also includes a system for spinning. Generally, if you’re looking for a spinning effect for the UI, you use the predetermined result from the server and then the client makes it “act random” but land on the aforementioned predetermined choice.
You can watch the video @TwiistedRoyalty provided, but I can’t stand watching something lighter than white itself.
Basically, you would like to have a predefined table of information consisting of the chances you have, what the item is, and information about it. Then, once you have this information filled in, you would like to call a function that chooses randomly based on the chance the player has for each item.
An example structure could look like this:
Code
local Crates = {
--// Basic crate
Basic = {
{
chance = 15,
item = "game.Path.To.Tool1"
},
{
chance = 30,
item = "game.Path.To.Tool2"
},
{
chance = 1,
item = "game.Path.To.Tool3"
},
};
--\\Basic crate
};
Next, you would like to create a function which collects each of the items possible to get from Cases.Basic
and put them into a table with the value of the chance, and the index of this value will be the index from the value of Cases.Basic
.
This is accomplished like so:
Code
function CreateChanceTable (originalTable)
local ChanceTable = {};
local SumChances = 0;
for index, value in pairs(originalTable) do
-- index here is the current position of the "originalTable"
-- while value is the current value of the "originalTable"
ChanceTable[index] = value.chance
-- We'll use this later..
SumChances = SumChances + value.chance;
end
-- Returning the freshly created ChanceTable, the sum of all the chances
-- and of course the originalTable
return ChanceTable, SumChances, originalTable;
end
Now, we have created the most basic of our system. Now, the plan for the rest is to create a system that will generate a random number between 1 and the sum of all the chances from the CreateChanceTable
function. We will do this last though, as we need to create a function that will get the value that belongs to the chance chosen.
What this code does is that it will go through each chance possible in the ChanceTable
and check if the ChosenChance
is between the chance. Using the Crates.Basic
table above, these two chances are “15” an “30”. So if we say that our ChosenChance = 12
, the function will check if “12” is more than or equal to lastChance
and less than the sum of lastChance + thisChance
.
Here's the code
function GetBelongingValue (chosenChance, crateTable)
local lastChance = 1;
for index, value in pairs (crateTable) do
-- thisChance is the chance of the current value
local thisChance = value.chance;
-- Checks if the chosen chance is belonging to the current chance in the crateTable
local isBelonging = (chosenChance >= lastChance and chosenChance < (lastChance + thisChance));
lastChance = lastChance + thisChance;
if isBelonging == true then return value, index, crateTable; end;
end
end
That was the tough part
Now, finally, it’s time to create a function that will utilize all the functions above and make it modular so we can use it with multiple tables (granted they have the same keys and values, of course).
Last piece
function SpinCrate (crateTable)
-- ChanceTable is the table we got when calling CreateChanceTable
-- SumChances is the sum of all the chances
local ChanceTable, SumChances = CreateChanceTable(crateTable);
-- Here we're choosing a random chance between 1 and SumChances
local chosenChance = math.random(1, SumChances);
-- Returns 3 values directly from GetBelongingValue, which are
-- The value, the index and the table
return GetBelongingValue(chosenChance, crateTable);
end
Happy? No, we still have to use the SpinCrate
function. Now, I promise, the last piece of code.
local chosen = SpinCrate(Crates.Basic);
print("Chose", chosen.item);
Finally, it’s done.
Below is the whole code for this:
Whole code
local Crates = {
--// Basic crate
Basic = {
{
chance = 15,
item = "game.Path.To.Tool1"
},
{
chance = 30,
item = "game.Path.To.Tool2"
},
{
chance = 1,
item = "game.Path.To.Tool3"
},
};
--\\Basic crate
};
function CreateChanceTable (originalTable)
local ChanceTable = {};
local SumChances = 0;
for index, value in pairs(originalTable) do
ChanceTable[index] = value.chance
SumChances = SumChances + value.chance;
end
return ChanceTable, SumChances, originalTable;
end
function GetBelongingValue (chosenChance, crateTable)
local lastChance = 1;
for index, value in pairs (crateTable) do
local thisChance = value.chance;
local isBelonging = (chosenChance >= lastChance and chosenChance < (lastChance + thisChance));
lastChance = lastChance + thisChance;
if isBelonging == true then return value, index, crateTable; end;
end
end
function SpinCrate (crateTable)
local ChanceTable, SumChances = CreateChanceTable(crateTable);
local chosenChance = math.random(1, SumChances);
local chosenItem = GetBelongingValue(chosenChance, crateTable);
return GetBelongingValue(chosenChance, crateTable);
end
SpinCrate(Crates.Basic)
I really hope I helped you, if you got any questions, be sure to reply and I will be happy to help.
EDIT: Woops, I forgot to provide you with more information, my bad.
Basically, this will only help you choose a random item from a table. When it comes the spinning itself, you need to create a GUI yourself. Here’s how a crate system (most of them, at least).
- Client requests to spin a crate
- Server validates input and checks conditions
- Server gets a random value from a table, and sends to the client
- Server also caches the item they won in case the player gets disconnected, this way the player won’t lose the item they actually won.
- The client plays animations for the player, it already knows what it will get, but it still plays to make the player addicted
- After a while, the animation start slowing down and the item that you won (which was decided previously) is being shown in the end
- After it is done with the animation, it tells the server, so that the server can show the item in the player’s inventory.