Taller Lua

Catálogo/Guardar monedas

EconomíaAvanzado

Guardar monedas

Carga y guarda leaderstats.Monedas. En Studio hay que activar API Services. En el juego publicado funciona solo.

Valores

Dónde va

  1. 01

    API Services

    Home → Game Settings → Security → Enable Studio Access to API Services.

  2. 02

    Publica el lugar

    Los DataStores no funcionan en un archivo local sin publicar.

  3. 03

    Script

    Pega en ServerScriptService. Crea leaderstats si no existen.

Explorer → ServerScriptServiceScriptGuardarMonedas

GuardarMonedas.luaScript
--[[
  Guardar monedas · Taller Lua
  Pega este Script en ServerScriptService y pulsa Play.
  Es para TU juego en Roblox Studio — no es un exploit.
  Activa API Services en Game Settings
]]

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local STORE_NAME = "TallerMonedas_v1"
local store = DataStoreService:GetDataStore(STORE_NAME)

local function keyFor(player)
	return "player_" .. player.UserId
end

local function setup(player)
	local stats = player:FindFirstChild("leaderstats")
	if not stats then
		stats = Instance.new("Folder")
		stats.Name = "leaderstats"
		stats.Parent = player
	end
	local coins = stats:FindFirstChild("Monedas")
	if not coins then
		coins = Instance.new("IntValue")
		coins.Name = "Monedas"
		coins.Parent = stats
	end

	local ok, data = pcall(function()
		return store:GetAsync(keyFor(player))
	end)
	if ok and typeof(data) == "number" then
		coins.Value = data
	else
		coins.Value = 0
	end
end

local function save(player)
	local stats = player:FindFirstChild("leaderstats")
	local coins = stats and stats:FindFirstChild("Monedas")
	if not coins then
		return
	end
	pcall(function()
		store:SetAsync(keyFor(player), coins.Value)
	end)
end

Players.PlayerAdded:Connect(setup)
Players.PlayerRemoving:Connect(save)

game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		save(player)
	end
end)

for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(setup, player)
end