Curious on how to change the value of a value inside a data store

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

Something like

function awardPoints(player, pointsAmount)
    local playerData = data[player.UserId]
    assert(playerData, "Cannot award points before player data is loaded.")
    playerData.Points += pointsAmount
end
1 Like

Always read the documentation. Roblox has an entire site dedicated to documenting every event/function of their services

In your case of trying to update a value in the datastore, I’d recommend the :UpdateAsync() function on GlobalDatastores.

Everything you need regarding the datastore service can be found in this documentation.

1 Like

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 :sunglasses: (if you did write them in one line)

1 Like

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