2

I'm trying to make a system that saves a value so that if the player has that value then he gains the powers of that particular item but I'm not able to make that happen. There are no errors in the output and the powers are not forwarded to the player, how can I make the Data store system work to save this boolean value? There are probably errors in my code. The code is below.

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)
takezo
  • 101
  • 7

1 Answers1

0

To save a boolean in a datastore, you can directly save a bool value into a data store. To do this, save it as usual:

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

If that doesn't work, try using 1s and 0s as a substitute, 1 would be true and 0 would be false. Code as below:

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

Hope it helps! Happy coding!

zeyuan2009
  • 36
  • 4