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)
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
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.
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.