首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Roblox-如何在roblox数据存储中存储大型数组

Roblox-如何在roblox数据存储中存储大型数组
EN

Stack Overflow用户
提问于 2018-09-24 19:19:44
回答 3查看 2.4K关注 0票数 1

我试图做一个游戏,玩家创造自己的建筑,然后可以保存他们为其他玩家看到和发挥。但是,roblox不允许我存储整个创建所需的所有数据(每个块都有几个属性),我得到的全部错误代码是: 104:无法在DataStore中存储数组

任何帮助都将不胜感激!

EN

Stack Overflow用户

回答已采纳

发布于 2018-09-25 10:19:46

我不确定这是不是最好的方法,但这是我的尝试。下面是一个表的示例,您可以使用表来存储多个值。我认为您可以使用HttpService的JSONEncode函数将表转换为字符串(希望能够更有效地保存)。

JSONEncode (将布里克的数据放入字符串中,您可以将其保存到DataStore

代码语言:javascript
复制
local HttpService = game:GetService("HttpService")

-- this is an example of what we'll convert into a json string
local exampleBrick = {
    ["Size"] = Vector3.new(3,3,3),
    ["Position"] = Vector3.new(0,1.5,0),
    ["BrickColor"] = BrickColor.new("White")
    ["Material"] = "Concrete"
}

local brickJSON = HttpService:JSONEncode(exampleBrick)
print(brickJSON)

-- when printed, you'll get something like
-- { "Size": Vector3.new(3,3,3), "Position": Vector3.new(0,1.5,0), "BrickColor": BrickColor.new("White"), "Material": "Concrete"}
-- if you want to refer to this string in a script, surround it with two square brackets ([[) e.g. [[{"Size": Vector3.new(3,3,3)... }]]

JSONDecode (读取字符串并将其转换回砖块)

代码语言:javascript
复制
local HttpService = game:GetService("HttpService")
local brickJSON = [[ {"Size": Vector3.new(3,3,3), "Position": Vector3.new(0,1.5,0), "BrickColor": BrickColor.new("White"), "Material": "Concrete"} ]]

function createBrick(tab)
    local brick = Instance.new("Part")
    brick.Parent = <insert parent here>
    brick.Size = tab[1]
    brick.Position= tab[2]
    brick.BrickColor= tab[3]
    brick.Material= tab[4]
end

local brickData = HttpService:JSONDecode(brickJSON)
createBrick(brickData) --this line actually spawns the brick

如果您想要解释任何可能的数据存储错误,也可以将该函数包装在pcall中。

将整个模型编码为字符串

如果你的玩家的“建筑”是一个模型,你可以使用上面的代码脚本将模型中的所有部分转换成一个json字符串来保存。

代码语言:javascript
复制
local HttpService = game:GetService("HttpService")
local StuffWeWantToSave = {}

function getPartData(part)
    return( {part.Size,part.Position,part.BrickColor,part.Material} )
end

local model = workspace.Building --change this to what the model is
local modelTable = model:Descendants()
for i,v in pairs(modelTable) do
    if v:IsA("Part") or v:IsA("WedgePart") then
        table.insert(StuffWeWantToSave, HttpService:JSONEncode(getPartData(modelTable[v])))
    end
end

将字符串解码为一个完整的模型

当服务器加载播放机的数据时,可能会发生这种情况。

代码语言:javascript
复制
local HttpService = game:GetService("HttpService")
local SavedStuff = game:GetService("DataStoreService"):GetDataStore("blabla") --I don't know how you save your data, so you'll need to adjust this and the rest of the scripts (as long as you've saved the string somewhere in the player's DataStore)

function createBrick(tab)
    local brick = Instance.new("Part")
    brick.Parent = <insert parent here>
    brick.Size = tab[1]
    brick.Position= tab[2]
    brick.BrickColor= tab[3]
    brick.Material= tab[4]
end

local model = Instance.new("Model") --if you already have 'bases' for the players to load their stuff in, remove this instance.new
model.Parent = workspace

for i,v in pairs(SavedStuff) do
    if v[1] ~= nil then
        CreateBrick(v)
    end
end

FilteringEnabled

如果您的游戏使用过滤启用,请确保只有服务器处理保存和加载数据!!(您可能已经知道了)如果您希望播放器通过单击gui按钮进行保存,则将gui按钮设置为一个RemoteFunction,该将其基数据发送到服务器,并将其转换为字符串。

顺便说一句,我不太擅长编写脚本,所以我可能犯了一个错误。祝你好运

票数 2
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52485973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档