I'll create a crate opening system with both client and server components. Here's a complete implementation:
Server Script (ServerScriptService)
-- CrateService (ServerScript)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
-- Create RemoteEvents
local crateRemotes = Instance.new("Folder")
crateRemotes.Name = "CrateRemotes"
crateRemotes.Parent = ReplicatedStorage
local openCrateRemote = Instance.new("RemoteEvent")
openCrateRemote.Name = "OpenCrate"
openCrateRemote.Parent = crateRemotes
local getCrateItemsRemote = Instance.new("RemoteFunction")
getCrateItemsRemote.Name = "GetCrateItems"
getCrateItemsRemote.Parent = crateRemotes
-- Crate configuration
local CRATE_ITEMS = {
{name = "Common Sword", rarity = "Common", weight = 50, color = Color3.fromRGB(169, 169, 169)},
{name = "Rare Shield", rarity = "Rare", weight = 25, color = Color3.fromRGB(0, 162, 255)},
{name = "Epic Bow", rarity = "Epic", weight = 15, color = Color3.fromRGB(163, 53, 238)},
{name = "Legendary Staff", rarity = "Legendary", weight = 8, color = Color3.fromRGB(255, 128, 0)},
{name = "Mythic Crown", rarity = "Mythic", weight = 2, color = Color3.fromRGB(255, 215, 0)},
{name = "Common Potion", rarity = "Common", weight = 45, color = Color3.fromRGB(169, 169, 169)},
{name = "Rare Gem", rarity = "Rare", weight = 20, color = Color3.fromRGB(0, 162, 255)},
{name = "Epic Armor", rarity = "Epic", weight = 12, color = Color3.fromRGB(163, 53, 238)},
{name = "Legendary Sword", rarity = "Legendary", weight = 5, color = Color3.fromRGB(255, 128, 0)},
{name = "Mythic Wings", rarity = "Mythic", weight = 1, color = Color3.fromRGB(255, 215, 0)}
}
-- Function to select weighted random item
local function selectRandomItem()
local totalWeight = 0
for _, item in pairs(CRATE_ITEMS) do
totalWeight = totalWeight + item.weight
end
local randomValue = math.random(1, totalWeight)
local currentWeight = 0
for _, item in pairs(CRATE_ITEMS) do
currentWeight = currentWeight + item.weight
if randomValue <= currentWeight then
return item
end
end
return CRATE_ITEMS[1] -- Fallback
end
-- Generate items for the spinning animation (includes the chosen item at the end)
local function generateSpinItems(chosenItem)
local items = {}
-- Add 20 random items for the spin
for i = 1, 20 do
table.insert(items, CRATE_ITEMS[math.random(1, #CRATE_ITEMS)])
end
-- Add the chosen item at the end
table.insert(items, chosenItem)
return items
end
-- Handle crate opening
openCrateRemote.OnServerEvent:Connect(function(player)
local chosenItem = selectRandomItem()
local spinItems = generateSpinItems(chosenItem)
-- Send the items and chosen item back to client
openCrateRemote:FireClient(player, spinItems, #spinItems)
end)
-- Handle getting crate items for display
getCrateItemsRemote.OnServerInvoke = function(player)
return CRATE_ITEMS
end
Client Script (StarterPlayerScripts)
-- CrateClient (LocalScript)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local SoundService = game:GetService("SoundService")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
-- Wait for remotes
local crateRemotes = ReplicatedStorage:WaitForChild("CrateRemotes")
local openCrateRemote = crateRemotes:WaitForChild("OpenCrate")
local getCrateItemsRemote = crateRemotes:WaitForChild("GetCrateItems")
-- Create GUI
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "CrateGui"
screenGui.Parent = playerGui
-- Main frame
local mainFrame = Instance.new("Frame")
mainFrame.Size = UDim2.new(0.8, 0, 0.6, 0)
mainFrame.Position = UDim2.new(0.1, 0, 0.2, 0)
mainFrame.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
mainFrame.BorderSizePixel = 0
mainFrame.Parent = screenGui
-- Title
local titleLabel = Instance.new("TextLabel")
titleLabel.Size = UDim2.new(1, 0, 0.15, 0)
titleLabel.Position = UDim2.new(0, 0, 0, 0)
titleLabel.BackgroundTransparency = 1
titleLabel.Text = "CRATE OPENING"
titleLabel.TextColor3 = Color3.white
titleLabel.TextScaled = true
titleLabel.Font = Enum.Font.GothamBold
titleLabel.Parent = mainFrame
-- Scroll frame for items
local scrollFrame = Instance.new("ScrollingFrame")
scrollFrame.Size = UDim2.new(1, -20, 0.6, 0)
scrollFrame.Position = UDim2.new(0, 10, 0.2, 0)
scrollFrame.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
scrollFrame.BorderSizePixel = 0
scrollFrame.ScrollBarThickness = 0
scrollFrame.CanvasSize = UDim2.new(0, 0, 0, 0)
scrollFrame.ScrollingDirection = Enum.ScrollingDirection.X
scrollFrame.Parent = mainFrame
-- Selection indicator
local indicator = Instance.new("Frame")
indicator.Size = UDim2.new(0, 120, 1, 0)
indicator.Position = UDim2.new(0.5, -60, 0, 0)
indicator.BackgroundTransparency = 1
indicator.BorderSizePixel = 3
indicator.BorderColor3 = Color3.fromRGB(255, 255, 0)
indicator.Parent = scrollFrame
-- Open crate button
local openButton = Instance.new("TextButton")
openButton.Size = UDim2.new(0.3, 0, 0.15, 0)
openButton.Position = UDim2.new(0.35, 0, 0.82, 0)
openButton.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
openButton.BorderSizePixel = 0
openButton.Text = "OPEN CRATE"
openButton.TextColor3 = Color3.white
openButton.TextScaled = true
openButton.Font = Enum.Font.GothamBold
openButton.Parent = mainFrame
-- Variables
local isSpinning = false
local itemFrames = {}
-- Function to create item frame
local function createItemFrame(item, index)
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0, 100, 1, 0)
frame.Position = UDim2.new(0, (index - 1) * 110, 0, 0)
frame.BackgroundColor3 = item.color
frame.BorderSizePixel = 2
frame.BorderColor3 = Color3.fromRGB(255, 255, 255)
frame.Parent = scrollFrame
local nameLabel = Instance.new("TextLabel")
nameLabel.Size = UDim2.new(1, 0, 0.6, 0)
nameLabel.Position = UDim2.new(0, 0, 0.2, 0)
nameLabel.BackgroundTransparency = 1
nameLabel.Text = item.name
nameLabel.TextColor3 = Color3.white
nameLabel.TextScaled = true
nameLabel.Font = Enum.Font.Gotham
nameLabel.TextWrapped = true
nameLabel.Parent = frame
local rarityLabel = Instance.new("TextLabel")
rarityLabel.Size = UDim2.new(1, 0, 0.2, 0)
rarityLabel.Position = UDim2.new(0, 0, 0.8, 0)
rarityLabel.BackgroundTransparency = 1
rarityLabel.Text = item.rarity
rarityLabel.TextColor3 = Color3.white
rarityLabel.TextScaled = true
rarityLabel.Font = Enum.Font.GothamBold
rarityLabel.Parent = frame
return frame
end
-- Function to clear items
local function clearItems()
for _, frame in pairs(itemFrames) do
frame:Destroy()
end
itemFrames = {}
end
-- Function to setup spin animation
local function setupSpin(items, chosenIndex)
clearItems()
-- Create item frames
for i, item in pairs(items) do
local frame = createItemFrame(item, i)
table.insert(itemFrames, frame)
end
-- Update canvas size
scrollFrame.CanvasSize = UDim2.new(0, #items * 110, 0, 0)
-- Calculate final position (center the chosen item)
local finalPosition = (chosenIndex - 1) * 110 - (scrollFrame.AbsoluteSize.X / 2) + 50
-- Start spinning animation
local spinTween = TweenService:Create(
scrollFrame,
TweenInfo.new(3, Enum.EasingStyle.Quart, Enum.EasingDirection.Out),
{CanvasPosition = Vector2.new(finalPosition, 0)}
)
-- Reset position
scrollFrame.CanvasPosition = Vector2.new(0, 0)
-- Start spin
spinTween:Play()
-- Handle completion
spinTween.Completed:Connect(function()
isSpinning = false
openButton.Text = "OPEN CRATE"
openButton.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
-- Highlight the won item
local wonFrame = itemFrames[chosenIndex]
if wonFrame then
local highlightTween = TweenService:Create(
wonFrame,
TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, -1, true),
{BorderColor3 = Color3.fromRGB(255, 255, 0)}
)
highlightTween:Play()
end
end)
end
-- Handle crate opening
openButton.MouseButton1Click:Connect(function()
if isSpinning then return end
isSpinning = true
openButton.Text = "SPINNING..."
openButton.BackgroundColor3 = Color3.fromRGB(170, 170, 0)
openCrateRemote:FireServer()
end)
-- Handle server response
openCrateRemote.OnClientEvent:Connect(function(items, chosenIndex)
setupSpin(items, chosenIndex)
end)
-- Initialize with preview items
spawn(function()
local previewItems = getCrateItemsRemote:InvokeServer()
if previewItems then
for i = 1, 10 do
local randomItem = previewItems[math.random(1, #previewItems)]
createItemFrame(randomItem, i)
end
scrollFrame.CanvasSize = UDim2.new(0, 10 * 110, 0, 0)
end
end)
Features:
Server Side:
- Weighted random item selection based on rarity
- Secure reward determination
- Generates spinning sequence with chosen item at the end
Client Side:
- Smooth scrolling animation that slows down and stops on the chosen item
- Visual feedback with item colors based on rarity
- Preview of available items when GUI loads
- Prevents multiple simultaneous spins
- Highlights the won item with a pulsing border
GUI Elements:
- Horizontal scrolling frame showing items
- Selection indicator in the center
- Open crate button with state feedback
- Item frames showing name, rarity, and color-coded backgrounds
The system uses TweenService for smooth animations and includes proper client-server communication to ensure fair and secure crate opening mechanics.