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

How do i store Data? And why is not my script storing(it doesnt print any error)?

local plrs = game:GetService("Players")
local dataservice = game:GetService("DataStoreService")
local moneystorage = dataservice:GetDataStore("MoneyStorage")

function loaddata(player)
	local id = "Money-"..player.UserId
	local data = nil
	local sucess, err = pcall(function()
		data = moneystorage:GetAsync(id)
	end)
	
	if data ~= nil then
		local moneyzinho = player.leaderstats:FindFirstChild("Money")
		if moneyzinho then
			moneyzinho.Value = data
			print("Data Loaded")
		end	
	else
		print("Data is unavailable/inexistent")
	end
	print(err)	
	print(sucess)
end

function savedata(player)
	local id = "Money-"..player.UserId
	local sucess, err = pcall(function()
		local moneyzinhoo = player.leaderstats:FindFirstChild("Money")
		if moneyzinhoo then
			moneystorage:SetAsync(id, moneyzinhoo.Value)		
			end
		end)
	if sucess then
		print("Data saved dw buddy")
	end
print(sucess)	
end
game:BindToClose(function()
	for _, player in pairs(plrs:GetChildren()) do
		if player then
			player:Kick("Game is shutting down! Take a clip if you have medal if your data didnt store.")
		end
	end
	wait(1)	
end)


plrs.PlayerAdded:Connect(loaddata)
plrs.PlayerRemoving:Connect(savedata)

1 answer

Ok here is a better way. Put this script in server script service.

local DSS = game:GetService("DataStoreService")

local DataStore = DSS:GetDataStore("Theking66haydayDatastoreSystem")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")

	leaderstats.Name = "leaderstats"-- Don't rename this

	leaderstats.Parent = player

	

	local Money = Instance.new("IntValue")

	Money.Name = "Money"
	Money.Parent = leaderstats

	

	local data

	local success, errorMessage = pcall(function()

		data = DataStore:GetAsync(player.UserId)

	end)

	if success and data ~= nil then

		print("Data successfully loaded!")

		Money.Value = data.Money

	else

		warn(errorMessage)

	end

end)



game.Players.PlayerRemoving:Connect(function(player)

	local leaderstats = player.leaderstats

	local data = {

		Money = leaderstats.Money.Value

	local success, errorMessage = pcall(function()

		DataStore:SetAsync(player.UserId,data)

	end)

	if success then

		print("Data successfully saved!")

	else

		warn(errorMessage)

	end

end)


This is a simple datastore system this should work!!

2 comments

0

Yes. I did enable API. Btw if my method of storing data is not worth it please tell me a better one.

0

I updated my post. I might of made typing errors let me know if there is any hope this helps

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