Tables and Dataservice

local dataservice = game:GetService("DataStoreService")
local datastore = dataservice:GetDataStore("Main",1)


local main = {
	textPro = false,
	textYay = true
}


game.Players.PlayerAdded:Connect(function(plr)
	local Success, Data = pcall(function()
		print("Finding plr data")
		return datastore:GetAsync(plr.UserId)
	end)
	
	if Success then
		if Data then
			print("Loading data")
			main[plr.UserId] = Data
			print("Loaded data for "..plr.Name)
		else
			print("No data ")
		end
		
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	print("Plr leaving saving data")

	local Success, err = pcall(function()

		datastore:SetAsync(plr.UserId, main)
		print("Saved data for "..plr.Name)
	end)
end)

This code doesn’t execute what it’s supposed to do, when the player joins it only prints “Finding plr data”, this tells me that its fetching the data. I thought it could be that I just have no data to load but it doesn’t print “No Data” so there must be something wrong with my code. Does anyone know how to help me fix this?

Try putting a else after if Success then…

Like this

    if Success then
		if Data then
			print("Loading data")
			main[plr.UserId] = Data
			print("Loaded data for "..plr.Name)
		else
			print("No data ")
		end

    else

        print(data)
		
    end
1 Like

Wasn’t the issue I figured out what was wrong with it. Thanks anyways!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.