How would i access this value?

I’m just curious on how id access the value in my datastore. I wanna add lets say 1000 points to it if a player buys a dev product or if they in a group maybe. I’m just completely lost on how id access “Points” inside the datastore and then add the 1000 points after the player buys the products or once they join the game if they’re in said group. I’m just used to regular leaderboard style.

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,      --------- This is what im trying to add to
			["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

Create an Attribute, Assign its Value to points, and create a Changed signal and change the data from there:

player:SetAttribute("Points", data[player.UserId]["Points"]) -- creates an Attribute for "points" if it does not exist

player:GetAttributeChangedSignal("Points"):Connect(function() -- Changed Signal
    data[player.UserId]["Points"] = player:GetAttribute("Points") -- New Value to points
end)

To Change the Value, you would do:

player:SetAttribute("Points", player:GetAttribute("Points") + Amount)

Just making sure would i add it into the same function? and then the other changing the value in a separate function when lets say the player buys the product?

Yes:

if success then 
	print("Data retrieved")
	if not playerData then
		print("New player, giving default data")
		playerData = {
			["Points"] = 1000,      --------- This is what im trying to add to
			["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
    player:SetAttribute("Points", data[player.UserId]["Points"]) -- creates an Attribute for "points" if it does not exist

player:GetAttributeChangedSignal("Points"):Connect(function() -- Changed Signal
    data[player.UserId]["Points"] = player:GetAttribute("Points") -- New Value to points
end)
end 

And yes.

Does the line that adds the points need to be in the same script? Or can i put in a seperate script?

No, It Connects a Signal to the Attribute for the Server to Listen to, when it changes, it will change the Value, With the only Expection being if its on a different Context Level (Server or Client)

Works on all Scripts besides the ones on a Different Context Level

For Example, you set it up on the Server, will detect Server Changes,

If on the Client, it will only detect Client Changes (usually)

So i ran it and the only error i got was attempt to perform arithmetic (add) on nil and number. Its happens on the line that adds the points.

God People are stupid. I get the same response everytime

Amount is the Amount you are changing, Replace it with the number you want to add

I put 1000 and thats the error that i got

So, you did this?

player:SetAttribute("Points", player:GetAttribute("Points") + 1000)

Yes i did it exactly like that

Can you show where in the Script its located?

Are you talking about where i put the line that adds the points? Or where the script itself is located in game?

I’m going to scream

My fault i totally read it completely wrong

This is how i have it in a seperate script

Players.PlayerAdded:Connect(function(player)
    if MarketplaceService:UserOwnsGamePassAsync(player.UserId, 15689499) then


	player:SetAttribute("Points", player:GetAttribute("Points") + 1000)
    end
end)

You are firing it too early then, because you have a loop to get Data, it hasn’t created the Attribute yet because of that

How would i fix that exactly? with a wait or?

You could do this:

Players.PlayerAdded:Connect(function(player)
    repeat wait() until player:GetAttribute("Points")
    if MarketplaceService:UserOwnsGamePassAsync(player.UserId, 15689499) then


	player:SetAttribute("Points", player:GetAttribute("Points") + 1000)
    end
end)

THANK YOU. It worked exactly how i was hoping

1 Like