Development
Code
Scripts and systems I've written for Roblox games.
-- Gives every player a Gold leaderstat when they join
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Value = 0
gold.Parent = leaderstats
end)
-- Weighted loot roll + animated chest lid + gold reward
local COOLDOWN = 10
local OPEN_TWEEN_TIME = 0.6
local LOOT_TABLE = {
{ name = "Common", gold = 10, weight = 60 },
{ name = "Rare", gold = 50, weight = 25 },
{ name = "Epic", gold = 150, weight = 12 },
{ name = "Legendary", gold = 500, weight = 3 },
}
local function rollLoot()
local total = 0
for _, item in ipairs(LOOT_TABLE) do total += item.weight end
local roll = math.random(1, total)
for _, item in ipairs(LOOT_TABLE) do
roll -= item.weight
if roll <= 0 then return item end
end
end
local Chest = {}
Chest.__index = Chest
function Chest.new(model)
return setmetatable({ Model = model, IsOpen = false, IsOnCooldown = false }, Chest)
end
function Chest:Animate()
local lid = self.Model:FindFirstChild("Lid")
local open = lid.CFrame * CFrame.Angles(math.rad(-110), 0, 0)
game:GetService("TweenService"):Create(lid, TweenInfo.new(OPEN_TWEEN_TIME), { CFrame = open }):Play()
end
function Chest:GiveReward(player, loot)
local gold = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Gold")
if gold then gold.Value += loot.gold end
end
function Chest:Open(player)
if self.IsOpen or self.IsOnCooldown then return end
self.IsOpen = true
self.IsOnCooldown = true
local loot = rollLoot()
self:Animate()
self:GiveReward(player, loot)
end
RockQuestDialogue.lua โ LocalScript
-- NPC dialogue with camera tween, typewriter text and accept/decline
local Player = game.Players.LocalPlayer
local TweenSvc = game:GetService("TweenService")
local Camera = workspace.CurrentCamera
local DialogueUI = Player.PlayerGui.Dialogue2.Dialogue
local NPC = workspace.RockNpc.NPC
local TaskGiven = false
NPC.HumanoidRootPart.Interact.Triggered:Connect(function(p)
if p ~= Player then return end
if not TaskGiven then
-- Show dialogue, tween camera to NPC
DialogueUI.Visible = true
DialogueUI.Option1.Text = "Accept"
DialogueUI.Option2.Text = "Decline"
Player.Character.Humanoid.WalkSpeed = 0
Camera.CameraType = Enum.CameraType.Scriptable
-- Typewriter effect
local msg = "Bring me a shiny blue rock from the cave!"
task.spawn(function()
for i = 1, #msg do
DialogueUI.DialogueText.Text = msg:sub(1, i)
task.wait(0.05)
end
end)
DialogueUI.Option1.MouseButton1Click:Once(function()
TaskGiven = true
workspace.Rock.Transparency = 0
end)
end
end)
-- Seeded daily shop: deterministic per day, 80% common / 20% rare
local shop = require(game.ReplicatedStorage.Modules.Shop)
local OFFSET = 0
local currentDay, currentItems = nil, {}
local function getItems(day)
local rng = Random.new(day)
local common, rare = {}, {}
for name, data in pairs(shop.DailyShop) do
if data.Rarity == "Common" then table.insert(common, name)
elseif data.Rarity == "Rare" then table.insert(rare, name) end
end
local result = {}
for i = 1, 3 do
local pool = (rng:NextNumber() < 0.2 and #rare > 0) and rare or common
local idx = rng:NextInteger(1, #pool)
local item = pool[idx]
local price = rng:NextInteger(math.floor(shop.DailyShop[item].Price/2), shop.DailyShop[item].Price)
table.insert(result, { Name = item, Price = price })
table.remove(pool, idx)
end
return result
end
while true do
local day = math.floor((os.time() + OFFSET) / (60*60*12))
if day ~= currentDay then
currentDay = day
currentItems = getItems(day)
game.ReplicatedStorage.DailyItemsShop:ClearAllChildren()
shop.AddDailyItems(currentItems)
end
task.wait(1)
end