如何在DataStore系统中保存BoolValue



我正在尝试创建一个保存值的系统,这样如果玩家拥有该值,他就会获得该特定物品的力量,但我无法实现这一点。输出中没有错误,功率也没有转发给玩家,我如何让数据存储系统工作来保存这个布尔值?我的代码中可能有错误。代码如下。

local DataStoreService = game:GetService("DataStoreService") -- pegar serviço
local DataStore = DataStoreService:GetDataStore("PlrData") -- setar as informações onde serão guardadas
local FrutaSalva = DataStoreService:GetDataStore("Fruit")
local ReplicatedS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService") -- runservice
game.Players.PlayerAdded:Connect(function(Plr) -- se o player entrar
local data = Instance.new("Folder", Plr) -- criar pasta no player que guarda valores
data.Name = "Data"

local exp = Instance.new("IntValue", data) -- valor
exp.Name = "Exp"
exp.Value = 1

local levels = Instance.new("IntValue", data)-- valor
levels.Name = "Level"
levels.Value = 1

local Points = Instance.new("IntValue", data)
Points.Name = "Points"
Points.Value = 3


local expneed = Instance.new("IntValue", data)-- valor
expneed.Name = "ExpNeeded"
expneed.Value = 100

local strenght = Instance.new("IntValue", data)-- valor
strenght.Value = 1
strenght.Name = "Strenght"

local Health = Instance.new("IntValue", data)-- valor
Health.Value = 100
Health.Name = "Health"

local char = Plr.Character or Plr.CharacterAdded:Wait()
local charhealth = char.Humanoid.Health

local DevilFruit = Instance.new("IntValue", data)-- valor
DevilFruit.Value = 1
DevilFruit.Name = "DevilFruit"

local PlayerId = Plr.UserId -- id do jogador
local PlayerData = DataStore:GetAsync(PlayerId) -- informações do jogador são pegas e se estiverem salvas então 
local FruitGet = FrutaSalva:GetAsync("PlayerId")

if PlayerData then
strenght.Value = PlayerData["Strenght"] -- coloca o valor correspondente da informação salva na informação atual
Health.Value = PlayerData["Health"] -- coloca o valor correspondente da informação salva na informação atual
DevilFruit.Value = PlayerData["DevilFruit"] -- coloca o valor correspondente da informação salva na informação atual
exp.Value = PlayerData["Exp"] -- coloca o valor correspondente da informação salva na informação atual
levels.Value = PlayerData["Level"] -- coloca o valor correspondente da informação salva na informação atual
expneed.Value = PlayerData["ExpNeeded"]
Points.Value = PlayerData["Points"]-- coloca o valor correspondente da informação salva na informação atual
end
end)
local function tab(Player) -- cria uma table com o nome de status do player
local PlrStats = {} -- table criada
for _, v in pairs(Player.Data:GetChildren()) do -- procura os valores na pasta Data no player
PlrStats[v.Name] = v.Value -- pega os valores e os coloca na table de acordo com o valor e o nome
end
return PlrStats -- não sei (pesquisar)
end

game.Players.PlayerRemoving:Connect(function(Plr) -- conecta a função ao player sair
local PlrStats = tab(Plr) -- PlrStats é a função enviada com o parametro de player
local Sucess, Result = pcall(function() -- função safe
local PlrId = Plr.UserId -- player id
DataStore:SetAsync(PlrId, PlrStats) -- seta os valores da table plrstats no id do player para salvar
end)
if not Sucess then -- erro no save alerta
print(Result)
warn("Erro no save (Preocupante)")
end
end)
game:BindToClose(function() -- qunado o jogo fechar pelo servidor salva as info
for _, Player in pairs(game.Players:GetPlayers()) do
task.wait()
local PlrStats = tab(Player) -- PlrStats é a função enviada com o parametro de player
local Sucess, Result = pcall(function() -- função safe
local PlrId = Player.UserId -- player id
DataStore:SetAsync(PlrId, PlrStats) -- seta os valores da table plrstats no id do player para salvar
end)
if not Sucess then -- erro no save alerta
print(Result)
warn("Erro no save (Preocupante)")
end
end
end)
game.Players.PlayerAdded:Connect(function(Player) -- se o xp for maior que o necessário ou igual
task.wait(0.1) -- então um nivel aumenta e o xp necessário é maior
local Level = Player.Data.Level
local Exp = Player.Data.Exp
local ExpNeed = Player.Data.ExpNeeded
local Points = Player.Data.Points
local Strenght = Player.Data.Strenght
local Health = Player.Data.Health

local DevilFruit = Player.Data:WaitForChild("DevilFruit", 12)
local Char = Player.CharacterAdded:Wait()

local Hum = Char:WaitForChild("Humanoid")

Hum.MaxHealth = Hum.MaxHealth + Health.Value
task.wait(.1)
Hum.Health = Hum.Health + Health.Value


RunService.Heartbeat:Connect(function()
if Exp.Value == 0 and ExpNeed.Value == 0 then
Exp.Value = 1
ExpNeed.Value = 100
end
if Points.Value > 300 then
Points.Value = 300
end
if Exp.Value >= ExpNeed.Value then
Exp.Value = Exp.Value - ExpNeed.Value
Level.Value += 1
ExpNeed.Value *= 1.13
Points.Value += 3
end
if Strenght.Value == 0 or DevilFruit.Value == 0 or Health.Value == 0 then
Strenght.Value = 1
DevilFruit.Value = 1
Health.Value = 1
end
if Strenght.Value > 100 or DevilFruit.Value > 100 then
Strenght.Value = 100
DevilFruit.Value = 100
end
if Health.Value > 1000 then
Health.Value = 1000
end
end)

RunService.Heartbeat:Connect(function()
if Level.Value >= 100 then
Level.Value = 100
end
end)
end)
ReplicatedS:WaitForChild("Frutas"):WaitForChild("Fruta").OnServerEvent:Connect(function(Player, Fruit)
if FrutaSalva == true then
if Player.GomuGomu.Value == true then
ReplicatedS:WaitForChild("Frutas"):WaitForChild("Poderes"):WaitForChild("GomuPowers").Parent = Player.Character
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, result = pcall(function()
FrutaSalva:SetAsync(Player.UserId, true)
end)
if success then
FrutaSalva:SetAsync(Player.UserId, true)
else print(result)
end
end)

要在数据存储中保存布尔值,可以直接将布尔值保存到数据存储中。要做到这一点,请照常保存:

local DSS = game:GetService("DataStoreService")
local DS  = DSS :GetDataStore("MyDataStore")
local success,err = pcall(function()
DS:SetAsync("key",true)
end
if success then
print("Success!")
else
error("Error: "..err)
end

如果这不起作用,请尝试使用1和0作为替代,1将为true,0将为false。代码如下:

local success,err = pcall(function()
DS:SetAsync("key",1)
end

希望它能有所帮助!编码快乐!

最新更新