Here's a basic collection system script for Roblox, designed to track collected items and update a GUI. This system includes:
- Item Collection Logic (Workspace/Item Script)
- Leaderstats Tracking (Server Script)
- GUI Display (Local Script in StarterGui)
1. Item Collection Logic
Place this script inside a Part (e.g., named CollectiblePart) in the Workspace:
-- Script inside CollectiblePart
local part = script.Parent
local collected = false -- Prevents double collection
part.Touched:Connect(function(hit)
if collected then return end -- Exit if already collected
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local collectedValue = leaderstats:FindFirstChild("Collected")
if collectedValue then
collectedValue.Value += 1 -- Increment collection count
collected = true -- Mark as collected
part:Destroy() -- Remove the item
end
end
end
end)
Setup for the Item:
- Name the Part
CollectiblePart.
- Anchor it (
Anchored = true).
- Add a Mesh/Texture to make it visible.
2. Leaderstats Setup
Place this script in ServerScriptService:
-- ServerScriptService: Leaderstats Setup
game.Players.PlayerAdded:Connect(function(player)
-- Create leaderstats folder
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Create a collection counter
local collected = Instance.new("IntValue")
collected.Name = "Collected"
collected.Value = 0
collected.Parent = leaderstats
end)
3. GUI Display
Create a ScreenGui in StarterGui with a TextLabel:
- ScreenGui →
Name = "CollectionGUI"
- TextLabel →
Name = "CollectedLabel", Text = "Collected: 0"
Add this LocalScript inside the ScreenGui:
-- LocalScript inside CollectionGUI
local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local collectedValue = leaderstats:WaitForChild("Collected")
local textLabel = script.Parent:WaitForChild("CollectedLabel")
-- Update GUI when collected count changes
collectedValue.Changed:Connect(function()
textLabel.Text = "Collected: " .. collectedValue.Value
end)
Optional Enhancements
- Respawn Items: Instead of
part:Destroy(), hide it and respawn after a delay.
- Sound Effects: Add a
Sound instance to play when collected.
- Multiple Items: Duplicate
CollectiblePart across the map.
- Save Data: Use
DataStoreService to save progress.
Final Structure
Workspace
└── CollectiblePart
└── (Script for collection logic)
ServerScriptService
└── (Leaderstats setup script)
StarterGui
└── CollectionGUI
├── CollectedLabel
└── (LocalScript for GUI updates)
Let me know if you want advanced features like saving, item types, or visual effects! 🎮