如何编写一个 Roblox 脚本来保存玩家死亡时的背包和起始装备?

我编写了几个不同的脚本,但好像都不对。 基本上,我有一个商店,玩家可以从中购买工具和物品,但每次我测试并购买工具后,死亡后工具就会消失。我不希望人们一遍又一遍地购买。

下面是购买该工具和该工具所在位置的脚本。

script.Parent.MouseButton1Click:connect(function()
local RS = game:GetService('ReplicatedStorage')
local item = RS:WaitForChild('GravityCoil')
local price = 0
local player = game.Players.LocalPlayer
local stats = player:WaitForChild('leaderstats')

if stats.Cash.Value >= price then
    stats.Cash.Value = stats.Cash.Value - price
    local cloned = item:Clone()
    local cloned2 = item:Clone()
    cloned2.Parent = player.Backpack
end
end)
点赞
用户7942480
用户7942480

不要使用复制的存储方式。因为这样你就不能往里面添加数据,而每次客户端重新进入游戏时都需要重新从存储中加载数据。我过去常常使用“Lighting”来存储数据,但现在已经有了新的存储系统。不过,如果你不想每次玩家进入游戏时都重新加载数据,可以在一个你喜欢的存储系统中创建一个模型,以角色的名字命名,并通过购买系统,将背包中的物品保存到该模型中每次添加或移除物品。然后,当玩家重新生成时,将模型中的物品复制到背包中。我建议使用 ServerStorage 替代。然后,对于玩家模型中的每个物品,当他们重新生成时,将其复制到玩家的背包中。

script.Parent.MouseButton1Click:connect(function()
local RS = game:GetService('ServerStorage')
local item = RS:WaitForChild('GravityCoil')
local price = 0
local player = game.Players.LocalPlayer
local stats = player:WaitForChild('leaderstats')

if stats.Cash.Value >= price then
    stats.Cash.Value = stats.Cash.Value - price
    local cloned = item:Clone()
    local cloned2 = item:Clone()
    local plrMod = Instance.new("Model")
    plrMod.name = player.name
    plrMod.parent = RS
    cloned.parent = plrMod
    cloned2.Parent = player.Backpack
end
end)

然后,onRespawn 事件中的代码部分如下:

for child in plrMod
    child:Clone().parent = player

注意,这不是正确的语法,因为我已经有好几年没有做过 Lua 编程。

2018-08-02 22:12:42