Give owner money upon joining?

So I made a tower defense game following a GnomeCode tutorial and I want to give me, the owner, just infinite stars (the currency used to buy towers) when I join. The issue is, I have no idea how DataStores work so I’m not sure how I would do this and it’s not a leaderstat. Here is my code:

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 towers = require(replicatedStorage:WaitForChild("TowerShop"))

local MAX_SELECTED = 3
local data = {}

--load player 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("Connection success")
		if not playerData then
			print("New player, giving default data")
			playerData = {
				["Stars"] = 400,
				["SelectedTowers"] = {"Brute"},
				["OwnedTowers"] = {"Brute","Magician"},
			}
		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)
--save player data
local function saveData(player)
	if data[player.UserId] then
		local success = nil
		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", player.UserId)
		end
	else
		warn("No session data for", player.UserId)
	end
end
Players.PlayerRemoving:Connect(function(player)
	saveData(player)
	data[player.UserId] = nil
end)

Any help is appreciated, thank you!

1 Like

I think you would use the Command Bar when you’re playing the game to give yourself how ever many stars you want.

But it’s not a leaderstat it’s a table of data.

just use if player.UserId == game.OwnerId then

I did that but it didn’t work. This is what I wrote:

--load player data
local function loadData(player)
	local success = nil
	local playerData = nil
	local attempt = 1
	
	if player.UserId == 1317259399 then
		print("OWNER JOINED!")
		playerData = {
			["Stars"] = 9999,
			["SelectedTowers"] = {"Brute"},
			["OwnedTowers"] = {"Brute","Magician"},
		}
		data[player.UserId] = playerData
	end
	
	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 = {
				["Stars"] = 400,
				["SelectedTowers"] = {"Brute"},
				["OwnedTowers"] = {"Brute","Magician"},
			}
		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

It did print owner joined but it didn’t give me any stars in studio or the actual game. What should I do differently?

--load player data
local function loadData(player)
	local success
	local playerData
	local attempt = 1
	
	if player.UserId == 1317259399 then
		print("OWNER JOINED!")
		playerData = {
			["Stars"] = 9999,
			["SelectedTowers"] = {"Brute"},
			["OwnedTowers"] = {"Brute","Magician"},
		}
		data[player.UserId] = playerData
	end
	
	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 = {
				["Stars"] = 400,
				["SelectedTowers"] = {"Brute"},
				["OwnedTowers"] = {"Brute","Magician"},
			}
		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

Unfortunately, it still doesn’t work and isn’t that the same code just without stating nil and leaving it blank?

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 towers = require(replicatedStorage:WaitForChild("TowerShop"))

local MAX_SELECTED = 3
local data = {}

--load player 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("Connection success")
		if not playerData then
			print("New player, giving default data")
			playerData = {
				["Stars"] = 400,
				["SelectedTowers"] = {"Brute"},
				["OwnedTowers"] = {"Brute","Magician"},
			}
			if player.UserId == id then
				playerData = {
					["Stars"] = math.huge,
					["SelectedTowers"] = {"Brute"},
					["OwnedTowers"] = {"Brute","Magician"},
				}
			end
		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)
--save player data
local function saveData(player)
	if data[player.UserId] then
		local success = nil
		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", player.UserId)
		end
	else
		warn("No session data for", player.UserId)
	end
end
Players.PlayerRemoving:Connect(function(player)
	saveData(player)
	data[player.UserId] = nil
end)

Dang, it still doesn’t work but I think it’s because that code is in the “If not playerData” function as since I’ve tested this game obviously, I do have data to my ID.

you shouldn’t (have to) manually override it, just set it using datastoreservice in the command bar

Sorry to ask this, but what would I type? I’m not sure how to reference a players data in the command bar.

I’m not sure, you know your system

He doesn’t. GnomeCode made it (as said in the post).

if they just copied it like you said perhaps they should actually use that resource instead of just coming straight to scripting support

From this code it looks like you’re giving yourself 9999 Stars, but you are STILL running the code which retrieves data from the datastore, so you’re instantly overriding what you’ve just done. Try moving it below, or adding an if statement to prevent the loading code from running entirely.

Oh wow that made everything work! Thank you so much for helping me realize such a simple error, looks like I still have a long way to go!

1 Like

Glad to help! Good luck with your game!