Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My SavaData script keeps giving me the same error? need help?

Asked by 3 years ago

Okay so im trying to save this stringvalue in a folder in players location but i keep getting this error:

11:15:18.591 Argument 2 missing or nil - Server - MainData:49

idk why im getting this but i do need help

heres script:

local service = game:GetService("DataStoreService")
local datastore = service:GetDataStore("Stats")
local function save(player)

    local success,errormessage = pcall(function()

            local SaveData = {}

            for i,stats in pairs(player["Data Folder"]:GetChildren()) do

                SaveData[stats.Name] = stats.Value
            end 
        datastore:SetAsync(player.UserId,SaveData)
        end)

        if not success then 
        return datastore
        end         
end
game.Players.PlayerAdded:Connect(function(player)
    print'Joined'
    local datafolder = Instance.new("Folder",player)
    datafolder.Name = "Data Folder"

    local hakifolder = Instance.new("Folder",workspace:WaitForChild(player.Name))
    hakifolder.Name = "Buso"

    local fruitvalue = Instance.new("StringValue",datafolder)
    fruitvalue.Name = "Fruit"
    wait(.1)
    if not player:WaitForChild'Data Folder' then return end
    if player:WaitForChild'Data Folder'.Fruit.Value == "" then
        player["Data Folder"].Fruit.Value = "None" -- changes the value of it
    end
    print'loaded data folder'
    local data
    local success, errormessage = pcall(function()
        data = datastore:SetAsync(player.UserId)
    end)

    if success then
        for i,stats in pairs(datafolder:GetChildren()) do

            stats.Value = data[stats.Name]      
        end
        print("Success")
    else
        print("failed")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    save()
end)

help B)

0
sorry for the title i need to do that ReaperMr 0 — 3y

1 answer

Log in to vote
0
Answered by
Wiscript 622 Moderation Voter
3 years ago

On line 38, you call SetAsync() I am guessing to retrieve data. You need to change that to GetAsync

Therefore to fix this, simply do:

local service = game:GetService("DataStoreService")
local datastore = service:GetDataStore("Stats")
local function save(player)

    local success,errormessage = pcall(function()

            local SaveData = {}

            for i,stats in pairs(player["Data Folder"]:GetChildren()) do

                SaveData[stats.Name] = stats.Value
            end 
        datastore:SetAsync(player.UserId,SaveData)
        end)

        if not success then 
        return datastore
        end         
end
game.Players.PlayerAdded:Connect(function(player)
    print'Joined'
    local datafolder = Instance.new("Folder",player)
    datafolder.Name = "Data Folder"

    local hakifolder = Instance.new("Folder",workspace:WaitForChild(player.Name))
    hakifolder.Name = "Buso"

    local fruitvalue = Instance.new("StringValue",datafolder)
    fruitvalue.Name = "Fruit"
    wait(.1)
    if not player:WaitForChild'Data Folder' then return end
    if player:WaitForChild'Data Folder'.Fruit.Value == "" then
        player["Data Folder"].Fruit.Value = "None" -- changes the value of it
    end
    print'loaded data folder'
    local data
    local success, errormessage = pcall(function()
        data = datastore:GetAsync(player.UserId)
    end)

    if success then
        for i,stats in pairs(datafolder:GetChildren()) do

            stats.Value = data[stats.Name]      
        end
        print("Success")
    else
        print("failed")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    save()
end)
0
Imma try this rn :) ReaperMr 0 — 3y
0
i get a whole new error: 11:31:51.298 502: API Services rejected request with error. HTTP 403 (Forbidden) - Server - MainData:49 what happened? Thanks for responding to btw ReaperMr 0 — 3y
0
now im getting this O_O ServerScriptService.DATA.MainData:38: ServerScriptService.DATA.MainData:32: attempt to index nil with 'Fruit' ReaperMr 0 — 3y
Ad

Answer this question