What im tryna do is increase the value of the players points inside the data store whenver they buy a devproduct or are in a group. But im completely lost on how to access the points inside the datastore since its obviously not like a leaderboard. Any help is appreciated cause im a little lost
Heres my code i can provide the entire script if needed
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"},
["OwnedTowers"] = {"Sword Player","Rocket Noob"}
}
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
function awardPoints(player, pointsAmount)
local playerData = data[player.UserId]
assert(playerData, "Cannot award points before player data is loaded.")
playerData.Points += pointsAmount
end
just a little nitpick, but you can write your pcall a little better. If you didnt already know, pcall takes a function as its first arg, and the rest is args which would be passed into the function.
Now how to do it? It’s fairly simple, as this is all you need to put
local passed, playerData = pcall(
database.GetAsync, -- We get the function, witch is GetAsync
database, -- Without the :, we are required to pass self. Aka database
player.UserId -- The args needed for the database:GetAsync()
)
You can also apply this to SetAsyncs, and others that uses oop or just regular functions. What you did isnt wrong but this way of writing them is much more easier, plus its all in 1 line (if you did write them in one line)