Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

1 comment

0

sorry for the title i need to do that

1 answer

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)

4 comments

0

Imma try this rn :)

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

0

now im getting this O_O ServerScriptService.DATA.MainData:38: ServerScriptService.DATA.MainData:32: attempt to index nil with 'Fruit'

This discussion is preserved as it was. New answers, comments, and votes are closed.