Help with Roblox Studio Code

I’m creating a script that I found on GnomeCodes video where he creates a tower defence game. I’m onto a part where it comes to saving data. I’ve already tried changing the code but nothing seems to work.

The main problem is that when I leave the game normally, it saves my data and prints, ‘Data Saved Successfully’ but when I alter my ingame equips which changes the data, it doesn’t say that line and it doesn’t save the data. The problem is in the saveData function.

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

local dataBase = DataStoreService:GetDataStore("dataBase")
local towers = require(ReplicatedStorage:WaitForChild("TowerShop"))

local defaultData = {
	["Tokens"] = 150,
	["SelectedTowers"] = {"DirtThrower"},
	["OwnedTowers"] = {"DirtThrower", "Gunner"}
}

local MAX_SELECTED_TOWERS = 2

local data = {}

local success = nil

Players.PlayerAdded:Connect(LoadData)

local function saveData(player)
	if data[player.UserId] then
		local playerData = nil
		local attempt = 1
		
		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.UserId)
		end
	end
end

Show me the code for when you save the data when you ‘alter ingame equips’.

I can imagine if the function is working for the playerAdded event then it should work if called properly for other events too

1 Like

So basically the data table is has three tables within it with owned towers, equipped towers and money and whenever they equip or buy a tower it changes the table. its meant to save the table with the return data[player.UserId] line. It says that data is saved when i dont make any changes to my equips but then when I do change my equips it doesnt say the data is saved and it doesn’t save it.

does it say “Unable to save data for …your userid

If you are testing in studio make sure to go to settings, security, and allow studio API requests.

No. It must be a problem with saving the data if the data is different then when they first initially joined.

Can you show the loaddata function to?

local function LoadData(player)
	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("Connection Success")
		if not playerData then
			print("New player, giving default data")
			playerData = {
				["Tokens"] = 150,
				["SelectedTowers"] = {"DirtThrower"},
				["OwnedTowers"] = {"DirtThrower", "Gunner"}
			}
		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
end

Players.PlayerAdded:Connect(LoadData)