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

This Table Datastore is not working as intended?

So I was doing some commissioned work for a friend of mine, and I was doing some table datastores for the kill/death ratio thing

local datastore = game:GetService("DataStoreService"):GetDataStore("SBdata")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	local kills = Instance.new("IntValue")
	kills.Parent = leaderstats
	kills.Name = "Kills"
	local deaths = Instance.new("IntValue")
	deaths.Parent = leaderstats
	deaths.Name = "Deaths"
	
	local dataTable = {}
	dataTable.kills = kills.Value
	dataTable.deaths = deaths.Value

	
	local success,failure = pcall(function()
		datastore:SetAsync(plr.UserId,dataTable)
	end)
	if success then
		print("Data saved")
	else
		warn("data failed to save")
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	local dataTable
	local success,failure = pcall(function()
		dataTable = datastore:GetAsync(plr.UserId)
	end)
	if success and dataTable then
		plr.leaderstats.Kills.Value = dataTable.kills
		plr.leaderstats.Deaths.Value = dataTable.deaths

		print("successfully loaded")
	elseif failure then
		warn("data failed to load")
	end
end)

I don't understand why this isn't working...

Am I doing something wrong?

1 answer

✓ Accepted answer

Hello.

Problem:

You mixed up :GetAsync() and :SetAsync(). When the player is added, you must use :GetAsync. When the player is leaving, you must use :SetAsync.

Recommendations:

  • Use :GetService() while indexing a service.

  • Print the error message if there's an error.

  • Add an extra "and" to the elseif statement checking if the pcall didn't succeed.

Fixed Code:

local datastore = game:GetService("DataStoreService"):GetDataStore("SBdata")

game:GetService("Players").PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Parent = plr
    leaderstats.Name = "leaderstats"

    local kills = Instance.new("IntValue")
    kills.Parent = leaderstats
    kills.Name = "Kills"

    local deaths = Instance.new("IntValue")
    deaths.Parent = leaderstats
    deaths.Name = "Deaths"

    local data

    local success,failure = pcall(function()
        data = datastore:GetAsync(plr.UserId)
    end)
    if success then
	kills.Value = data.kills
	deaths.Value = data.deaths
        print("Data loaded")
    else
		print(failure)
        warn("data failed to load")
    end
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)
    local dataTable = { kills = plr.leaderstats.Kills.Value, deaths = plr.leaderstats.Deaths.Value}

    local success,failure = pcall(function()
       datastore:SetAsync(plr.UserId, dataTable)
    end)
    if success and dataTable then

        print("successfully saved")
    elseif failure and not success then
		print(failure)
        warn("data failed to save")
    end
end)

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