Gems not saving between placed?

For context, I’m creating a tower defense game through a tutorial by GnomeCode, for reference, I’m on the last episode. I’m not fully sticking to the tutorial as I’ve made some differences like GUI and stuff. Anyways…

I’ve been stuck on a problem where the gems awarded are not being given to the users in the Lobby.

This is the DataStore script, in the “SavaData” function you can see where the gems are being awarded. This script should deal with all thing relating to data

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--local towers = require(ReplicatedStorage:WaitForChild("TowerShop"))
local database = DataStoreService:GetDataStore("database")
local RunService = game:GetService("RunService")
local functions = ReplicatedStorage:WaitForChild("Fun ctions")
local events = ReplicatedStorage:WaitForChild("Events")
local getDataFunc = functions:WaitForChild("GetData")
local exitEvent = events:WaitForChild("return")
local MAX_SELECTED_TOWERS = 5

local data = { 

}
local defaultData = {
	["Gems"] = 100,
	["SelectedTowers"] = {"Cutler"},
	["OwnedTowers"] = {"Cutler", "Gunslinger"}
}


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

	while not success and attempt < 4 do
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)
		if not success then
			attempt += 1
			warn(playerData)
			task.wait()
		end
	end
	--[[			Repeat until is often unreliable
	repeat
	until
	]]
	if success then
		print("Data Loaded Successfully")  --Why connection established??
		if not playerData then
			print("New player, giving default data")
			playerData= {
				["Gems"] = 100,
				["SelectedTowers"] = {"Cutler"},
				["OwnedTowers"] = {"Cutler", "Gunslinger"}
			}
		end
		data[player.UserId] = playerData
	else
		warn("Unable to get data for player", player.UserId)
		player:Kick("There was a problem loading your data! Message the developer on Roblox if this problem continues!")
	end
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 gems = math.round((info.wave / 2) * 10)
		if info.message.Value == "Victory!" then
			gems = 200
			
		end
		data[player.UserId].Gems = gems
		print("pLAYERS GOT", gems)
		while not success and attempt < 4 do
			success, playerData = pcall(function()
				database:SetAsync(player.UserId, data[player.UserId])
				--[[			UpdateAsync overcomplicates things in my opinion
				return database:UpdateAsync(player.UserId, function()
					return data[player.UserId]
				end)
				]]
			end)

			if not success then
				attempt += 1
				warn(playerData)
				task.wait()
			end
		end
		--[[				Again repeat until is often unreliable
		repeat
		until success == true or attempt == 3
		]]
		if success then
			print("Data saved successfully")

		else
			warn("Unable to save data for", player.UserId)
		end
	else
		warn("No Data for", player.UserId)
	end




end

exitEvent.OnServerEvent:Connect(function(plr)
	SaveData(plr)
	data[plr.UserId] = nil
end)
Players.PlayerRemoving:Connect(function(player)
	
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 the studio")
	end
	
end)

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

Maybe im doing something wrong? I haven’t gotten help in any other server so the devforum is my last resort!!!

Hmm maybe try

Players.PlayerRemoving:Connect(function(player)
	SaveData(player)
end)

I Would recommend using ProfileService though

if you want a ready-made profile service here it is:

But you will have to edit everything to your game’s stuff and Stats

Did it print Data saved successfully?

Above the removing event, there is a function that handles the exit button being clicked.

1 Like

I probably found out the issue

with this code, you are basically setting the value of their gems to whatever they got, not adding to their original coins

you should change that line to
data[player.UserId].Gems += gems

And perhaps a extra space between Fun and ctions

I Just fixed the gem issue, i forgot a .Value, and this also fixes another issue I had, thank you! :smiley:

1 Like

I’ve been meaning to change this for a while, and I will clean up all my code once i’m finished polishing the interactive parts of the game. :smiley:

1 Like

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