Confused on accessing datastore

So everything with my datastore transfers over fine. Im just confused on how to access a value inside my data store. For example i get how to access let say points and the points value but im not sure how id access something like Sword in lets say Datastore - SelectedWeapon - Sword and if they have a sword selected then place it in there inventory. Data store script below

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local database = DataStoreService:GetDataStore("database")
local functions = ReplicatedStorage:WaitForChild("Functions")
local events = ReplicatedStorage:WaitForChild("Events")
local getDataFunc = functions:WaitForChild("GetData")
local exitEvent = events:WaitForChild("ExitGame")



local data = {}






local function LoadData(player)
	local success = nil
	local playerData = nil
	local attempt = 1

	repeat
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)
		
		attempt += 1
		if not success then
			warn(playerData)
			task.wait()
		end
	until success or attempt == 3
	
	if success then 
		print("Data retrieved")
		if not playerData then
			print("New player, giving default data")
			playerData = {
				["Points"] = 1000,
				["SelectedTowers"] = {"Sword Player"},
				["SelectedSword"] = {"Sword"},
				["OwnedTowers"] = {"Sword Player","Rocket Noob"},
				["OwnedSwords"] = {"Sword"}

			}
			
		end
		data[player.UserId] = playerData
	else
		warn("Unable to get data for player", player.UserId)
		player:Kick("There was a problem getting your data")
	end
	player:SetAttribute("Points", data[player.UserId]["Points"]) -- creates an Attribute for "points" if it does not exist

	player:GetAttributeChangedSignal("Points"):Connect(function() -- Changed Signal
		data[player.UserId]["Points"] = player:GetAttribute("Points") -- New Value to points
	end)
	print(playerData)
end
Players.PlayerAdded:Connect(LoadData)




local function SaveData(player)
	if data[player.UserId] then
		local success = nil
		local playerData = nil
		local attempt = 1
		
		local info = workspace.Info
		local points = math.round(info.Wave.Value / 2)
		if info.Message.Value == "VICTORY" then
			points = 12
		end
		data[player.UserId].Points += points
		
		repeat
			success, playerData = pcall(function()
				return database:UpdateAsync(player.UserId, function()
					return data[player.UserId]
				end)
			end)

			attempt += 1
			if not success then
				warn(playerData)
				task.wait()
			end
		until success or attempt == 3

		if success then 
			print("Data saved successfully")
		else
			warn("Unable to save data for player", player.UserId)
		end
	else
		warn("No session data for", player.UserId)
	end
	
end
exitEvent.OnServerEvent:Connect(function(player)
	SaveData(player)
	data[player.UserId] = nil
end)

game:BindToClose(function()
	if not RunService:IsStudio() then
		for index, player in pairs(Players:GetPlayers()) do
		task.spawn(function()
			SaveData(player)
		end)
		end
	else
		print("Shutting down inside studio")
	end
end)


getDataFunc.OnServerInvoke = function(player)
	return data[player.UserId]
end

Hey! It will just be the same as accessing point! for example, if you want to get SelectedSword you can just do data[player.UserId]["SelectedSword"] and it will return an array of string!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.