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
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)
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?
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
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)
Players.PlayerAdded:Connect(function(player)
if MarketplaceService:UserOwnsGamePassAsync(player.UserId, 15689499) then
player:SetAttribute("Points", player:GetAttribute("Points") + 1000)
end
end)
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)