Datastore In-Game

How could I make a new instance in game and save it to datastore? So i want that i make it with command like /create Cash IntValue and it will produce IntValue with name Cash and automatic save to datastore?

What I ask is not about making the chat command but how to make new instance in game that save to datastore whether it’s intval, etc.

it would be
local Instance = Instance.new("NumberValue")
and the datastores: Datastores

or whatever value you want to have

some thing like that ?

or that?

first yes, that actually stores data

second one is a system to make Coin Models appear randomly on the map

1 Like

Yes so I just make datastores first maybe empty without any value then when i want to make value in game, on the script i will just use for i, v in pairs (abc:GetChildren()) do v.Value = datastore , like that? Like using regular with making the instance manual by script, but i use it for i, v in pairs so even if i want to make new instance value in game it will automatic direct it because it’s gonna be inside for loops.

i would suggest you to watch a tutorial video about datastores as this is how i learned datastores, it really helps more

Is it? Sorry if it’s hard to explain, because what i want to make is so Admin can make New Instance Value from in game and auto connect to datastore, so it’s more dynamic, rather than you write local blabla = Instance.Value(“TypeValue”, player) manually on roblox studio.

If I load the datastore using for i, v in pairs(Player:GetChildren()) do , when I make new instance (IntValue or etc) in game, is it gonna automatic connect to datastore?

no, the script only does as much as you tell it to do. once you’ve retracted the data from the Datastores, you need to continue using it and set an actual value with it.

Well this is a little out of topic, is it better to use Global DataStore(Regular) or DataStore2 or maybe you have any reference that is better than this two?

I mean, assuming there all in the same Folder you could do something like below. If not you could store a reference to each Value. The code below isn’t for actual game use, if you want it to be game ready I’d recommend optimizing and making it less ugly. Whipped it up quickly so you could understand the concept better. Not tested so not sure if it actually works, but I’m sure you can work stuff out. I tried to comment as much as I could so you could understand better.

Also I wouldn’t recommend storing the data right away, if you’re doing a lot of chat commands it can clog it up. Instead save it whenever the player leaves the game.

local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local GeneratedInstances = DSS:GetDataStore("Generated")


game.Players.PlayerAdded:Connect(function(plr)
	local Folder = Instance.new("Folder", plr) -- create new folder for where stuff is stored
	
	local Success, Result = pcall(function()
		return GeneratedInstances:GetAsync(plr.UserId) --pcall because roblox datastores fail sometime
	end)
	
	if Result ~= nil and typeof(Result) == "table" then -- make sure data existed before and make sure its not an error
		for _, v in pairs(Result) do
			local NameOfValue = v[1]
			local ActualValue = v[2]
			local Type = v[3]
			
			local NewValue = Instance.new(Type) -- cretae the value
			NewValue.Name = NameOfValue
			NewValue.Value = ActualValue
			NewValue.Parent = Folder
		end
	end
	
	plr.Chatted:Connect(function(msg) -- chat command
		if string.sub(msg, 1, 7) == "/create" then 
			local splitMessage = string.split(msg, " ")
			local NameOfProperty = splitMessage[2]
			local ActualProperty = splitMessage[3]
			
			local createdInstance = Instance.new(ActualProperty)
			createdInstance.Name = NameOfProperty
			createdInstance.Parent = plr.Folder 
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local AllGeneratedInstances = {} -- table where we save the data
	for i, v in pairs(plr.Folder:GetChildren()) do -- get the folder or wherever were saving the int values
		table.insert(AllGeneratedInstances, i, {v.Name, v.Value, v.className}) -- putting all relevant info in said table
	end
	GeneratedInstances:SetAsync(plr.UserId, AllGeneratedInstances) -- save to datastore
end)

Look at this tutorial I made!