Creating a Crate/Spin System

Hey, i’m creating a game and im thinking about use crates to unlock and get “Materials” to craft, how do i achieve this with this Crate system without having a limit of items?

This is a great example! Does not take much to convert it to work with multiple crates either! Just a few simple changes!

  • ServerScriptService > SpinnerServer

change line 16 from
function rfunction.OnServerInvoke(player)
to
function rfunction.OnServerInvoke(player, crate)

also change line 37 from
if ownedItems[item.Name] then
to
if ownedItems[item.Name] or item.Crate ~= crate then

So this would check for the item is owned, and if it belongs to the wrong crate.


  • ReplicatedStorage > SpinnerContents

just edit each item to include the crate…
old:
{Name = “Item1”, ImageId = 2419083546};
{Name = “Item2”, ImageId = 1468821126};

new:
{Name = “Item1”, ImageId = 2419083546, Crate = “Box1”};
{Name = “Item2”, ImageId = 1468821126, Crate = “Box2”};


Then lastly, in

  • StarterGui > SpinnerGUIs > SpinnerClient

when invoking, simply add/change the crate details…
local spinDetails, errorMessage = rfunction:InvokeServer(“Box2”)

7 Likes

Hi I don’t undrestand where do I put this code :

--Eliminate already-owned items (Optional)
local modifiedItemTypes = contents:GetItemTypes()
--[[
if contents.EliminateOwnedItems then
	--Get table of currently owned items
	local ownedItems = {}
	for i,v in pairs(player.leaderstats.Inventory:GetChildren()) do
		ownedItems[v.Name] = true
	end
	--Iterate through each group
	for groupIndex, group in pairs(modifiedItemTypes) do
		local totalItems = #group.Items
		local totalItemsPlusOne = totalItems + 1
		-- Iterate though Items back-to-front so the items remain in the same position if one is removed
		for i = 1, totalItems do
			local itemIndex = totalItemsPlusOne - i
			local item = group.Items[itemIndex]
			if ownedItems[item.Name] then
				table.remove(group.Items, itemIndex)
			end
		end
	end
end
--]]

--Generate Items
local items = {}
for i = 1, contents.Crates do
	--Determine rarity
	local cumulativeChance = 0
	local rarityGroup
	local randomChance = math.random()
	for groupIndex, group in pairs(modifiedItemTypes) do
		local chance = group.Rarity.Chance
		cumulativeChance = cumulativeChance + chance
		if randomChance <= cumulativeChance and #group.Items > 0 then
			rarityGroup = group
			break
		end
	end
	
	--If user has collected all items, return error message
	if not rarityGroup then
		return nil, "Error: unlocked all possible items!"
	end
	
	--Select random item from rarity group and add to 'items' table
	local newItemsGroup = rarityGroup.Items
	local newItemPos = math.random(1,#newItemsGroup)
	local newItem = newItemsGroup[newItemPos]
	table.insert(items, newItem)
end

Is there a good reason for handling generating the random items that come before the winning item on the server instead of the client?

As long as you deduce the winning item on the server, you’re fine to generate the others on the client. Some developers may store spin-related details only on the server, hence I’ve generated all the crates on the server for this example.

I know this is pretty late but if I understand what you are asking, you are asking how to make it so you can get the same item multiple times and be able to have multiple of that item.

SpinnerContents in ReplicatedStorage change line 15 to false

In SpinnerServer in ServerScriptService change on line 88

		--Original Code
		local newStat = Instance.new("ObjectValue")

to

		local newStat = Instance.new("NumberValue")

Right under that make a new line and put

		newStat.Value = 1 --how much you obtain for finding it

Line 85 change

		--Original Code 
		leaderstats.Cash.Value = leaderstats.Cash.Value + itemTypes[winningItem.GroupIndex].Rarity.DuplicateReward

to

		leaderstats.Inventory:FindFirstChild(winningItem.Name).Value =leaderstats.Inventory:FindFirstChild(winningItem.Name).Value +1 --how much you obtain for finding it

All that is left is to change the messages that show when you find a duplicate item in lines 23-24 in SpinnerContents, could make it so it just says +1 instead of +20 cash, whatever you desire it to be.

5 Likes

I’ve constantly thought about how-to do this, and made similar things (Not Including Spinning) Because honestly, I was completely oblivious; to where, you could show only portions of a Gui (Besides Sprite Sheets). Basically, I had no freaking clue that Clip Descendants could do this. Lol.

Thanks a lot!

1 Like

Would you make a system that works with this like an inventory system or a skin/weapon system?

1 Like

While it’s not completely supported, you should probably factor the ArePaidRandomItemsRestricted key from the return of GetPolicyInfoForPolicyAsync.

It’s future proofing for when they actually implement it, since lootboxes are prohibited in some countries, ie: Belgium

2 Likes

@TheBigC10

This may be something I explore in the future, thanks for the suggestion.


@FilteredDev

Lootboxes/spinners aren’t strictly prohibited, it’s the paid factor behind them that is. This tutorial focuses on the functioning and design behind a spinner, as apposed to its implementation, therefore ArePaidRandomItemsRestricted isn’t something I plan to include.

1 Like

Hello! I really like this method, but I have faced a problem here: The leaderstats aren’t DataStored, (meaning that the cash remaining, items and inventory won’t save after you leave the game) and when I add my own DataStored leaderstats with the same name it loads the number 10 or 15 in the 3 leaderstats. I hope you can help me fix that!

This tutorial is purely on the functioning and design behind a spinner, as apposed to saving the values used and generated within it.

I’m looking to release a module to assist with data saving, loading, etc in the upcoming months; for the time being you’ll have to create your own datastore system or explore pre-made ones such as DataStore2.

1 Like

I’m kinda confused as to what the variable spinDe is supposed to be. Should it be true?

(It’s a debounce I’m guessing)


EDIT:
Ok so I’m skipping ahead and I found this:

I want the player to be able to get them infinite amount of times. How would I do that?

There might be things where I want to change it, so please help!

EDIT 2: Oh wait.

do I just get rid of that line then?

1 Like

Loving this system! Well made and works just how i would want it to. Keep up the amazing work!

What about if you want to use this system and add multiple crates with different items?

Still need help on this. If you are still replying to this please tell me how I am supposed to do this! It’s been about 2 months!

1 Like
2 Likes

How could i add more crates? An older reply about that doesn’t seem to work for me

4 Likes

hello there! i know im really late here, but i wanted to ask a quite important question, Is there any way to make the Crate/Spin System give tools instead of just Decals? Would be appreciated if i can get an answer! Thank You.

3 Likes

How would make it so that it rewards the player, a tool to the starter pack/Backpack instead of a value in leaderstats? Sorry I’m new to this and your tutorial is the only one out there that actually make sense.