บันทึกข้อมูล

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

เกมมักจะต้องจัดเก็บข้อมูลถาวร ระหว่างเซสชัน เช่นระดับของผู้เล่นจุดประสบการณ์เงินสดรายการสินค้าคลังสถานที่และอื่นๆ

บทสอนนี้แสดงวิธีการสร้างคลังข้อมูลพื้นฐาน **** บันทึกข้อมูลผู้เล่น และอ่านข้อมูลกลับเป็นเซสชันเกม

เปิดใช้งานการเข้าถึงสตูดิโอ

โดยค่าเริ่มต้นแล้วเกมที่ทดสอบใน Studio ไม่สามารถเข้าถึงที่เก็บข้อมูลได้ ดังนั้นคุณต้องเปิดใช้งานก่อน:

  1. เลือก ไฟล์ และ ตั้งค่าเกม

  2. ในส่วน ความปลอดภัย เปิดใช้งาน เปิดการเข้าถึงสตูดิโอไปยังบริการ API และคลิก บันทึก

สร้างคลังข้อมูล

คลังข้อมูลต้องใช้ชื่อที่ไม่ซ้ำ ชื่อ ตัวอย่างนี้สร้างคลังข้อมูลที่ชื่อว่า PlayerGold ซึ่งบันทึกทองของผู้เล่นแต่ละคนในที่เก็บถาวร:

  1. สร้าง Script ภายใน ServerScriptService ที่เรียกว่า GoldManager

  2. คลังข้อมูลจะจัดการโดย DataStoreService ดังนั้นจึงได้รับบริการ:


    local DataStoreService = game:GetService("DataStoreService")
  3. โทร DataStoreService:GetDataStore() ด้วยสตริง "PlayerGold" .วิธีนี้จะเข้าถึงคลังข้อมูล PlayerGold หากมันมีอยู่แล้วหากไม่มีอยู่ เมธอดจะสร้างคลังข้อมูลใหม่และตั้งชื่อว่า PlayerGold


    local DataStoreService = game:GetService("DataStoreService")
    local goldStore = DataStoreService:GetDataStore("PlayerGold")

บันทึกข้อมูล

คลังข้อมูลเป็นเกือบสารานุกรมเช่นโต๊ะ Luauแต่ละค่าในคลังข้อมูลจะได้รับการจัดระเบียบโดยกุญแจที่ไม่ซ้ำกัน **** ซึ่งอาจเป็นกุญแจเฉพาะของผู้เล่น UserId หรือเพียงแค่สตริงที่มีชื่อสำหรับการโปรโมทเกม

KeyValue
3125060850
35167597920
50530609278000

เพื่อบันทึกข้อมูลผู้เล่นในคลังข้อมูล:

  1. สร้างตัวแปรชื่อ playerUserID สำหรับกุญแจคลังข้อมูล จากนั้นใช้ playerGold เพื่อเก็บจำนวนทองคำเริ่มต้นของผู้เล่น


    local DataStoreService = game:GetService("DataStoreService")
    local goldStore = DataStoreService:GetDataStore("PlayerGold")
    -- คีย์และมูลค่าของคลังข้อมูล
    local playerUserID = 505306092
    local playerGold = 250
  2. เพื่อบันทึกข้อมูลลงในคลังข้อมูล PlayerGold โทรหา SetAsync ภายในการโทรป้องกันโดยส่งตัวแปรคีย์และค่าที่สร้างไว้ก่อนหน้านี้


    local DataStoreService = game:GetService("DataStoreService")
    local goldStore = DataStoreService:GetDataStore("PlayerGold")
    -- คีย์และมูลค่าของคลังข้อมูล
    local playerUserID = 505306092
    local playerGold = 250
    -- ตั้งค่าคีย์ร้านข้อมูล
    local success, error = pcall(function()
    goldStore:SetAsync(playerUserID, playerGold)
    end)
    if not success then
    warn(error)
    end

ฟังก์ชันเช่น SetAsync() เป็นการโทรเครือข่ายที่อาจล้มเหลวเป็นครั้งคราวตามที่แสดงด้านบน pcall() จะใช้เพื่อตรวจจับและจัดการเมื่อเกิดความล้มเหลวดังกล่าว

ในรูปแบบพื้นฐานที่สุด pcall() รับฟังก์ชันและส่งคืนสองค่า:

  • สถานะซึ่งเป็น true หากฟังก์ชันดำเนินการโดยไม่มีข้อผิดพลาด หรือ false อย่างอื่น
  • มูลค่าการ返回ของฟังก์ชันหรือข้อความข้อผิดพลาด

ตัวอย่างด้านบนตรวจสอบสถานะในบรรทัดที่ 13 หาก SetAsync() ล้มเหลวด้วยเหตุผลใดก็ตาม ตัวอย่างจะแสดงข้อผิดพลาดในหน้าต่าง ผลผลิต

อ่านข้อมูล

เพื่ออ่านข้อมูลจากคลังข้อมูล โทร GetAsync() ด้วยชื่อกุญแจที่ต้องการ


local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
-- คีย์และมูลค่าของคลังข้อมูล
local playerUserID = 505306092
local playerGold = 250
-- ตั้งค่าคีย์ร้านข้อมูล
local setSuccess, errorMessage = pcall(function()
goldStore:SetAsync(playerUserID, playerGold)
end)
if not setSuccess then
warn(errorMessage)
end
-- อ่านคีย์ร้านข้อมูล
local getSuccess, currentGold = pcall(function()
return goldStore:GetAsync(playerUserID)
end)
if getSuccess then
print(currentGold)
end

เพื่อทดสอบสคริปต์ให้คลิกที่ รัน และสังเกตค่า currentGold ที่พิมพ์ลงในหน้าต่าง ออก โปรดทราบว่าอาจใช้เวลาสองสามวินาที เนื่องจากฟังก์ชันต้องเชื่อมต่อกับเซิร์ฟเวอร์คลังข้อมูล

อ่านและบันทึกอัตโนมัติ

สคริปต์ก่อนหน้านี้ทำงานได้ แต่มีปัญหาพื้นฐาน: มันรวมค่าที่ถูกเขียนล่วงหน้าสำหรับ playerUserID และ playerGold ดังนั้นจึงไม่สนับสนุนผู้เล่นหลายคนที่มีปริมาณทองแตกต่างกันโซลูชันที่สมจริงมากขึ้นอ่านมูลค่าทองเมื่อผู้เล่นเชื่อมต่อกับประสบการณ์แล้วจะบันทึกเมื่อผู้เล่นออกวิธีนี้หมายถึงการเชื่อมต่อคำร้องขอเก็บข้อมูลกับการเหตุการณ์ จากบริการ


local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
-- เพิ่มมูลค่าทองคำสำหรับผู้เล่นแต่ละคนในตารางท้องถิ่นเพื่อหลีกเลี่ยงการโจมตีข้อมูล
-- จัดเก็บซ้ำๆ
local playerGold = {}
local function incrementGold(player, amount)
playerGold[player.UserId] += amount
end
local function onPlayerAdded(player)
-- อ่านคีย์ร้านข้อมูล
local success, storedGold = pcall(function()
return goldStore:GetAsync(player.UserId)
end)
if success then
local currentGold
if storedGold then
currentGold = storedGold
else
currentGold = 0
end
playerGold[player.UserId] = currentGold
print(currentGold)
end
-- ทดสอบการเพิ่มทองคำ
incrementGold(player, 5)
end
local function onPlayerRemoving(player)
-- ตั้งค่าคีย์ร้านข้อมูล
local success, err = pcall(function()
goldStore:SetAsync(player.UserId, playerGold[player.UserId])
end)
if not success then
warn(err)
end
-- ล้างรายการเพื่อให้ตารางไม่เติบโตในช่วงชีวิตของเซิร์ฟเวอร์
playerGold[player.UserId] = nil
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

อ่านและบันทึกตำแหน่งตัวละคร

เพื่อบันทึกตำแหน่งผู้เล่นคุณทำงานกับ Character แทน Player แต่หลักการคล้ายกันครั้งนี้สร้าง Script ภายใน ServerScriptService ที่เรียกว่า PositionManager :


local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Workspace = game:GetService("Workspace")
local playerPositionStore = DataStoreService:GetDataStore("PlayerPositionStore")
local function positionHandler(player)
-- โหลดตำแหน่งบนตัวละครเพิ่ม
player.CharacterAdded:Connect(function(character)
local success, coords = pcall(function()
return playerPositionStore:GetAsync(player.UserId)
end)
local position = Vector3.new(coords[1], coords[2], coords[3])
if success and position then
character:PivotTo(CFrame.new(position))
print("Loaded player position!")
else
warn("Failed to load position for player " .. player.Name .. ". Placing in default position.")
end
-- จัดการการฟื้นคืนชีพของผู้เล่นเมื่อตาย
local humanoid = character:FindFirstChildOfClass("Humanoid")
humanoid.Died:Connect(function()
local spawnLocation = Workspace:FindFirstChild("SpawnLocation")
character:PivotTo(spawnLocation.CFrame)
end)
end)
-- บันทึกตำแหน่งเมื่อลบตัวละคร
player.CharacterRemoving:Connect(function(character)
local position = character:GetPivot().Position
local success, err = pcall(function()
playerPositionStore:SetAsync(player.UserId, {position.X, position.Y, position.Z})
print("Saved player position!")
end)
if not success then
warn("Failed to save position for player " .. player.Name .. ": " .. err)
end
end)
end
Players.PlayerAdded:Connect(positionHandler)

สคริปต์นี้เพิ่มคลังข้อมูลใหม่ playerPositionStoreเนื่องจากคลังข้อมูลเก็บเฉพาะประเภทพื้นฐานแทนที่จะเป็นวัตถุคุณต้องเก็บ X, Y และ Z ในฐานะตัวเลขแยกต่างหากแทนที่จะเป็นค่าเดียว Vector3 หรือ CFrameเมื่อคุณทดสอบประสบการณ์ของคุณ เคลื่อนย้ายตัวละครของคุณไปรอบๆสังเกตว่าตัวละครของคุณกลับไปยังตำแหน่งเดียวกันในครั้งต่อไปที่คุณทดสอบประสบการณ์

โปรเจกต์ตัวอย่าง

ตอนนี้คุณเข้าใจการใช้งานที่เก็บข้อมูลพื้นฐานแล้ว ลองทดสอบในเกมตัวอย่าง Gold Rushคุณยังสามารถแก้ไขเกมในสตูดิโอและสำรวจสคริปต์ GoldManager ที่ปรับปรุงแล้วซึ่งแสดงทองเป็นส่วนหนึ่งของอินเทอร์เฟซผู้ใช้และรวมถึงการบันทึกอัตโนมัติ