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

How do you save and upload data with DataStores?

when I load my player when trying to retrieve the data, I do not get an error or a success. on the other hand, the saving does work but randomly.

local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("Doesnotd Matterrigh now")

game.Players.PlayerAdded:Connect(function(player)
	
	local RetrieveData
	
	local success, errorMessage = pcall(function()

	end)
	if success then
		if RetrieveData then
			player:WaitForChild("leaderstats").Days.Value = DataStore:GetAsync(player.UserId,player.leaderstats.Days.Value)
			print("Retrived")

		else

		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	print(player," Has Left")
	local days = player.leaderstats.Days.Value

	local success, errorMessage = pcall(function()
		DataStore:SetAsync(player.UserId,days)

	end)
	if success then
		print("All Saved")
	else
		print(errorMessage)
	end
end)

1 comment

0

I re-wrote my answer, tested it, should work :)

1 answer

✓ Accepted answer

The whole point of a pcall is that it returns an error or a success in the function. You didn't write any code in the pcall, so there's no error or success to it.

local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("Doesnotd Matterrigh now")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local days = Instance.new("IntValue")
	days.Parent = leaderstats
	days.Name = "Days"
	
	
    local Days_Data

    local success, errorMessage = pcall(function()
		Days_Data = DataStore:GetAsync(player.UserId.. "days") --you should make it a keyword, rather than a value
    end)
	
    if success then
        days.Value = Days_Data        
        print("Retrived")
	elseif errorMessage then
		warn(errorMessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    print(player," Has Left")
    local days = player.leaderstats.Days.Value

    local success, errorMessage = pcall(function()
        DataStore:SetAsync(player.UserId.. "days", days) --key, keyword, value
    end)
    if success then
        print("All Saved")
    else
        warn(errorMessage) --warn is like print, except in orange text, showing where the warning is
    end
end)

Edit: re-wrote it, tested, works for me

3 comments

0

The Days data is not even getting saved when the player leaves the game.

1

Bro Thanks :)

0

no problem :)

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