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

Help with datastores?

I apologize if this seems like a request. I have been looking at it's wiki page, however I am too incompetent to make sense of it. Please can someone explain it slightly simpler with some code showing how to utilize it. For example: If there was a value in a player called Carpets how would I save it so when they rejoin/when the value changes it could load/save. (Example). Preferably a string explanation too.

1 answer

If want the** Auto-Save** Feature, do this is a script:

ds = game:GetService("DataStoreService"):GetDataStore("PlaceHolder's Name")
--Get Data Store Service

game.Players.PlayerAdded:connect(function(Player)
	--[[Setup Leaderstats]]
	local leaderstats = Instance.new("Model",Player)
	leaderstats.Name = "leaderstats"
	
	--[[Stats]]
	local stat1 = Instance.new("StringValue",leaderstats)
	--String Value(Text) or IntValue(Num no decs)
	stat1.Name = "Stat Name Here"
	stat1.Value = "Stat's Starting Value here"
	
	local stat2 = Instance.new("IntValue",leaderstats)
	--String Value(Text) or IntValue(Num no decs)
	stat1.Name = "Stat Name Here"
	stat1.Value = stat'svaluehere
end)

--[[Saving Stats]]
game.Players.PlayerRemoving:connect(function(Player)
	--Save the player's stats
	for i=1,c in pairs(Player:FindFirstChild("leaderstats"):GetChildren())
		ds:UpdateAsync(Player.Name, function(oldValue) return c.Value end)
	end
end)

game.OnClose = function()
	--Note:This is a callback Function the called before this server closes
	--For each player, do this
	for i,v in pairs(Players:GetChildren()) do
		--Save the stat for that player
        	for i=1,c in pairs(v:FindFirstChild("leaderstats"):GetChildren())
			ds:UpdateAsync(v.Name, function(oldValue) return c.Value end)
		end
    	end
	--Just incase XD
	wait(1)
end

Please give me credits. I built this for my obby which used advance technology for bug-free experience. If you don't believe this works, please visit my obby on my profile to see it work:

2 comments

0

Where does this script go?

0

Workspace, I don't know if it will work on server-script storage

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