Search code examples
luarobloxluau

Data Store not saving the Player's Boolean Value


I was making a game that when the Player dies, then the Player will get banned but isn't working (The part of Saving the Bool Value that checks if the player was banned already been banned). How i can fix that? No errors in output.

Code:

local DataStoreService = game:GetService("DataStoreService")
local BANS = DataStoreService:GetDataStore("BANNED")

game.Players.PlayerAdded:Connect(function(Player)
    local Char = Player.CharacterAdded:Wait() or Player.Character
    local Hum = Char:WaitForChild("Humanoid") or Char.Humanoid
    
    local banornot = Instance.new("Folder")
    banornot.Name = "Banned"
    banornot.Parent = Player
    
    local banned = Instance.new("BoolValue", banornot)
    banned.Name = "Banido"
    banned.Value = false
    
    local PlayerId = Player.UserId
    
    local BANIDO = BANS:GetAsync(PlayerId)
    
    Hum.Died:Connect(function()
        if not BANIDO then
            BANS:SetAsync(PlayerId, banned.Value)
            Player:Kick("You can't join the game.")
        end
    end)
    
end)

game.Players.PlayerAdded:Connect(function(Player)
    local PlayerId = Player.UserId
    
    local banidovalue = Player:WaitForChild("Banned"):WaitForChild("Banido").Value
    
    local BANIDO = BANS:GetAsync(PlayerId, banidovalue)
    
    if BANIDO then
        banidovalue = true
        if banidovalue == true then
            Player:Kick("You can't join the game. Sorry you know the rules.")
            else print("erro")
        end
        
    end
end)

Solution

  • There are multiple issues with this.

    The main issue is that BANIDO will never change. It will always have the value false, no matter what you do. Why? The only use of SetAsync takes the BoolValue's value, which also does not change. So, it's always false. There is a point in which you "set" it to true, except you don't. You take the value, put it into a variable, and set the variable to true. The actual BoolValue doesn't change. Even if you were to fix that, you kick the user right after that, which makes it useless.

    Second, you don't need a BoolValue. I don't know why you used it, so I can't explain this part.

    Here is a commented version of an improved script, in which I go through all of the issues:

    local DataStoreService = game:GetService("DataStoreService")
    local BANS = DataStoreService:GetDataStore("BANNED")
    
    game.Players.PlayerAdded:Connect(function(Player)
        local Char = Player.CharacterAdded:Wait() or Player.Character
        local Hum = Char:WaitForChild("Humanoid") --No "or Char.Humanoid" is needed, WaitForChild waits until it finds the thing which makes that part useless.
        
        --We don't need a BoolValue
            
        local PlayerId = Player.UserId
        
        local BANIDO = BANS:GetAsync(PlayerId)
        if BANIDO then --I moved the check from the other connection to this one. Do not connect to an event twice in the same script if the necessities are the same.
            Player:Kick("You can't join the game. Sorry you know the rules.")
        end
        --I did not do a check like this: BANIDO == true
        --This is because BANIDO is already a boolean, true or false, and you're essentially saying "if this is true, give me true, if it's false, give me false" which is just the value
        
        Hum.Died:Connect(function()
            if not BANIDO then
                BANS:SetAsync(PlayerId, true) --true instead of banned.Value. We got rid of the BoolValue, and also, even if we didn't, it wouldn't change.
                Player:Kick("You can't join the game.")
            end
        end)
        
    end)