Hello, I’m trying to make my very own TDS game with my friends, but we’ve encountered issues with data stores. We have already posted another post but we are still stuck. Our goal: save the towers in a table and save that, our scripts are edited from the original dev forum posts and we’ve tried our best but I don’t know if it works, or how to add towers, and how to check the towers correctly. I’m very new to datastores while having skills in scripting, I’m going to give you my scripts and I hope you can help me.
Module
local GameData = {}
local players = {}
function GameData:SetPlayerTowers(player, towers)
players[player.UserId] = towers
print(towers)
end
function GameData:AddPlayerTowers(player, towers)
table.insert(players, towers)
end
function GameData:GetTowers(player)
return players[player.UserId]
end
return GameData
server saving
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.PlayerRemoving:Connect(function(Player)
local Key = GenerateDataKey(Player)
local towers = {
Recruit = true;
--more but not going to show them
}
MyDataStore:SetAsync(Key, towers)
end)
Server (activating module stuff)
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, {
recruit = true
})
end)
-- Handle saving when a player leaves
game:GetService('Players').PlayerRemoving:Connect(function(player)
GameData:RemovePlayerTowers(player)
end)