Ad
Log in to vote
0

IntValues are 0 even if I gave them other values?

Asked by 6 days ago

Hello! I'm using a folder with values. They are going to be stored in the database after, but I have a problem. Even tho I set the values to 1 when I define them, if I run the game and look in explorer, their value is 0. This is what I tried:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataScore")

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)

    local ab_folder = Instance.new("Folder")
    ab_folder.Name = "abilities"
    ab_folder.Parent = player

    local points_lvl = Instance.new("IntValue")
    points_lvl.Name = "pointsbonus"
    **points_lvl.Value = 1**
    points_lvl.Parent = ab_folder

    local health_lvl = Instance.new("IntValue")
    health_lvl.Name = "health"
    **health_lvl.Value = 1**
    health_lvl.Parent = ab_folder

    local agility_lvl = Instance.new("IntValue")
    agility_lvl.Name = "agility"
    **agility_lvl.Value = 1**
    agility_lvl.Parent = ab_folder

    local saved_points_level
    local saved_health_level
    local saved_agility_level
    local succes, errormessage = pcall(function()

        saved_points_level = myDataStore:GetAsync(player.UserId.."-points_lvl")
        saved_health_level = myDataStore:GetAsync(player.UserId.."-health_lvl")
        saved_agility_level = myDataStore:GetAsync(player.UserId.."-agility_lvl")
    end)

    if succes then      
        points_lvl.Value = saved_points_level
        health_lvl.Value = saved_health_level
        agility_lvl.Value = saved_agility_level
    else
        print("There was an error whilst getting your data")
    end

end)

3 answers

Log in to vote
0
Answered by 6 days ago

you need to see if theres data. when theres no data saved, i think it will auto change to 0. right now you only have 'if succes then' and just change the values to the data which you dont have.

Ad
Log in to vote
0
Answered by 6 days ago
Edited 6 days ago

Make sure you save the data the player is achieving. DataStoreService will only work on a real server, not Roblox Studio.

Players.PlayerRemoving:Connect(function(Player)
    local UserId = Player.UserId
    local Functions = {
        function()
            return myDataStore:SetAsync(UserId .. "-points_lvl", Player["abilities"]["pointsbonus"].Value)
        end,
        function()
            return myDataStore:SetAsync(UserId .. "-health-lvl", Player["abilities"]["health"].Value)
        end,
        function()
            return myDataStore:SetAsync(UserId .. "-agility_lvl", Player["abilities"]["agility"].Value)
        end,
    }

    for index, value in pairs(Functions) do
        local success, response = pcall(value)
        if not response then warn("saving error: " .. tostring(response)) continue end
    end
end)
Log in to vote
0
Answered by 5 days ago

I tried but it still didn't work. Even if it doesn't save from Studio, they have values since I declared them, nothing should change them.

Answer this question