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

Why does this DataStore script not load, but save?

Hey! I am currently experiencing a Problem with DataStoreServices. I am using this script to save players money, Spawn etc. But it won't work, the saving works, but the loading does not. It would always think like SaveData[2] does not exist, so it sets the money to 50. It does not work in Studio nor in the Player. Any help with this?

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("InsertDataNameHere") --Not my real DataStore name tho
game.Players.PlayerAdded:Connect(function(plr)
	local SaveData = {}
	local SaveDataIns = Instance.new("Folder")
	SaveDataIns.Name = "SaveData"
	SaveDataIns.Parent = plr
	local MoneyIns = Instance.new("IntValue")
	MoneyIns.Name = "Money"
	MoneyIns.Parent = SaveDataIns
	local FirstVisit = Instance.new("BoolValue")
	FirstVisit.Name = "FirstVisit"
	FirstVisit.Parent = SaveDataIns
	local Spawn = Instance.new("StringValue")
	Spawn.Name = "Spawn"
	Spawn.Parent = SaveDataIns
	local VersionIns = Instance.new("IntValue")
	VersionIns.Name = "Version"
	VersionIns.Parent = SaveDataIns
	local HasApartmentIns = Instance.new("BoolValue")
	HasApartmentIns.Name = "HasApartment"
	HasApartmentIns.Parent = SaveDataIns
	
	local success = pcall(function()
		return DataStore:GetAsync(plr.UserId.."-savedata", SaveData)
	end)
	
	if success then
		print("Loaded " .. plr.Name.. "'s Data!")			
					if SaveData[1] then
			else
			SaveData[1] = false
			plr:FindFirstChild("SaveData").FirstVisit.Value = false
		end
		if SaveData[2] then
			else
			SaveData[2] = 50
			plr:FindFirstChild("SaveData").Money.Value = 50
			end
			if SaveData[3] then
			else
SaveData[3] = "Café"
			plr:FindFirstChild("SaveData").Spawn.Value = "Café"
			end
			if SaveData[4] then
			else
SaveData[4] = workspace.Version.Value
			plr:FindFirstChild("SaveData").Version.Value = workspace.Version.Value
			end
		    if SaveData[5] then
			else
SaveData[5] = false
			plr:FindFirstChild("SaveData").HasApartment.Value = false
			end
		
		MoneyIns.Value = SaveData[2]
		FirstVisit.Value = SaveData[1]
		Spawn.Value = SaveData[3]
		VersionIns.Value = SaveData[4]
		HasApartmentIns.Value = SaveData[5]
	else
		print("Error while loading.")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local Mon = plr:FindFirstChild("SaveData").Money.Value
	local FV = plr:FindFirstChild("SaveData").FirstVisit.Value
	local Spwnn = plr:FindFirstChild("SaveData").Spawn.Value
	local sers = plr:FindFirstChild("SaveData").Version.Value
	local HA = plr:FindFirstChild("SaveData").HasApartment.Value
	print(Mon.. " "..Spwnn.." "..sers.." ")
	local SaveData = {}
	SaveData[1] = FV
	SaveData[2] = Mon
		SaveData[3] =Spwnn
	SaveData[4] = sers
		SaveData[5] = HA
	local success = pcall(function()
		return DataStore:SetAsync(plr.UserId.."-savedata", SaveData)
	end)
	
	if success then
		print("Saved "..plr.Name.."'s Data!")
	end
end)

1 comment

0

Well.. This is an mess.. Why don't you use this, its easier, you don't have to do anything too! -- Saving Data game.Players.PlayerRemoving:connect(function(player) local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

local statstorage = player:FindFirstChild("leaderstats"):GetChildren() for i = 1, #statstorage do datastore:SetAsync(statstorage[i].Name, stats

2 answers

Roblox has a function that named datastore:GetAsync(userid). And you wasn't used it. Place it in playeradded function

MoneyIns.Value = DataStore:GetAsync(plr.UserId) or 0 

I got the error by myself. It was this part:

local success = pcall(function()
	        return DataStore:GetAsync(plr.UserId.."-savedata", SaveData)
	    end)

I replaced it with:

SaveData = DataStore:GetAsync(plr.UserId.."-savedata")

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