How to make a datastore like whats used in tds

is there a way you could do that with tables and tell the server which ones they have and not have dependent on that table

yea of course, just set the saved value of something like MiniGunner = false to —> MiniGunner = true when the player buys the item and using SetAsync and GetAsync you should be able to save and load your DataStore Values

I’m new to datastores how would i use setasync and getasync

you should first get the DataStoreService by typing

   local DSS = game:GetService("DataStoreService")

and Get your DataStore by typing

local MyDataStore = DSS:GetDataStore("HERE SHOULD BE YOUR DATASTORE NAME", "Players")

After that you should then make a function that creates a Key to every player’s DataStore
say it as the player has a key to his own house and he’s the only one that has the access to his home’s door cause of the key he owns and that’s exactly what we want to achieve here, we do that by typing

function GenerateDataKey(Player)
local Key = "UID_" .. Player.userId
     return Key
end

you can use the GetAsync() function to get the player’s Data using their generated Key

local Key = GenerateDataKey(player)
	local data = MyDataStore:GetAsync(Key)

we should also detect if the player has saved values or is he new to the game by saying:

if data then
-- Here you will have to input the player's saved data into an IntValue or something 
-- eg: Money.Value = data.Money
end

you will do all of this when the player joins the game, now lets say the player leaves and you want to save his data, here comes SetAsync(), you will to do that by also create a key for the player and getting the intvalues and saving them.

local Key = GenerateDataKey(Player)
	local Data = {Money.Value, MiniGunner.Value, etc}
	MyDataStore:SetAsync(Key, Data)

and that’s it for DataStore, hope you learned something new from this.

2 Likes

Im pretty sure i did this wrong but does it go like this

local DSS = game:GetService("DataStoreService")



game.Players.PlayerAdded:connect(function(Player)
	local MyDataStore = DSS:GetDataStore("HERE SHOULD BE YOUR DATASTORE NAME", "Players")

	local function GenerateDataKey(Player)
		local Key = "UID_" .. Player.userId
		return Key
	end

	local Key = GenerateDataKey(Player)
	local data = MyDataStore:GetAsync(Key)

	if data then
		-- Here you will have to input the player's saved data into an IntValue or something 
		-- eg: Money.Value = data.Money
	end

	local Key = GenerateDataKey(Player)

	local towers = {
		
	}

	MyDataStore:SetAsync(Key, data)
end)
  local DSS = game:GetService("DataStoreService")
local MyDataStore = DSS:GetDataStore("HERE SHOULD BE YOUR DATASTORE NAME", "Players")

local function GenerateDataKey(Player)
local Key = "UID_" .. Player.userId
return Key
end    

game.Players.PlayerAdded:connect(function(Player)

local Key = GenerateDataKey(Player)
local data = MyDataStore:GetAsync(Key)

if data then
	-- Here you will have to input the player's saved data into an IntValue or something 
	-- eg: Money.Value = data.Money
end
end)

game.Players.PlayerRemoved:Connect(function(Player)
     local Key = GenerateDataKey(Player)
     local towers = {
	
}
   MyDataStore:SetAsync(Key, data)
end)

Thanks, but now how would i make this work, how would other scripts see if i had the towers or not, and how could i update the list of tower

Look into ModuleScripts. This should be the primary way your game’s data gets managed.

Here’s an example:

-- GameData ModuleScript
local GameData = {}
local players = {}

function GameData:SetPlayerTowers(player, towers)
    players[player.UserId] = towers
end

function GameData:GetTowers(player)
    return players[player.UserId] or {}
end

return GameData

how would i call this from the server?

There’s a lot of tutorials on module scripts out there:

Google is your friend.

Here’s how you would use the above script when a player gets added:

local GameData = require(path.to.module)

game:GetService('Players').PlayerAdded:Connect(function(player)
  -- TODO: Try to get saved data from a datastore

  -- Set some default tower if theres no existing data for the player
  GameData:SetPlayerTowers(player, {
    SomeRandomTower = {
      ...
    }
  })
end)

-- Handle saving when a player leaves
game:GetService('Players').PlayerRemoving:Connect(function(player)
  -- TODO: Save the data to a datastore

  --[[
    Didn't make this method in the post above
    but ideally you would just delete the player from the players table
  ]]--
  GameData:RemovePlayerTowers(player)
end)

so my script is currently

local GameData = require(game.ReplicatedStorage.ModuleScript)

game:GetService('Players').PlayerAdded:Connect(function(player)
	-- TODO: Try to get saved data from a datastore

	-- Set some default tower if theres no existing data for the player
	GameData:SetPlayerTowers(player, {
		Frost = true
	})
end)

-- Handle saving when a player leaves
game:GetService('Players').PlayerRemoving:Connect(function(player)
	-- TODO: Save the data to a datastore

  --[[
    Didn't make this method in the post above
    but ideally you would just delete the player from the players table
  ]]--
	GameData:RemovePlayerTowers(player)
end)

but how do i give them towers because the changes i made don’t work, how do i give them a starter tower and how do i check which towers they own and don’t

That’s up to you to do I have no idea how your tower system works.

As for checking which they have:

if (GameData:GetTowers(player).Frost == true) then
  -- has Frost tower so do stuff
end

how do i add towers to their inventory remotely?

I don’t really know what you’re asking here. You would just add a new method to the GameData module called something like :AddTower()

how do i do that I’m confused?

table.insert() into the player in the game data

how should it work correctly??

you need to use datastores for saving people literally explained that to you above

do I need to table.insert(towers, tower)? and how would i give the info on the (tower) part?

image
Just store the tower in a table and save that table. then load it when it needs to be loaded

Also sorry for buffing a old thread(Dang post was already solved)