I am trying to make a script to save a value called "Reputation" and update the datastores when a player gets the increase (like at the end of a round) or when they're leaving. I used a block to test this out by giving me 10 Rep every time I touch it and it worked at first but the value wouldn't save. All the code 'checkpoints,' if you will, gave me success messages but the values still don't save. I have tried rewriting it several times and I have searched for examples to go off of and I cannot seem to get it to work. I was trying to get it to index a player with a value of 1 if they didn't exist but this is proving to be rather tricky. I even watch the value of the IntValue stay at 0 even though the code shows it at 10 when using a breakpoint. I think I might just not be finding the correct value for reputation but I figured I could still submit it here to see if I could save some time. Below is my code for the leaderboard:
--Datastore--
local Datastore = game:GetService("DataStoreService") --gets service
local GameStatStorage = Datastore:GetDataStore("gamestats") --creates a link to use for accessing the datastore
game.Players.PlayerAdded:Connect(function(plr) --when a player joins
local folder = Instance.new("Folder") --Makes folder for stats
folder.Parent = plr --places it
folder.Name = "leaderstats" --names it
local reputation = Instance.new("IntValue") -- makes a place to store the value of the players reputaion
reputation.Parent = folder --places the value container
reputation.Name = "Reputation"
local success, err = pcall(function() --wraps in pcall
GameStatStorage:GetAsync(plr.UserId.."-Reputation") --gets the players data
end)
if success then --if good then
print "Successfully Loaded" --Success message
plr.leaderstats.Reputation.Value = success --sets visible value to match
else --or if player doesnt have data to pull
plr.leaderstats.Reputation.Value = 1 -- then give them a value of zero to display
GameStatStorage:SetAsync(plr.UserId, 1) --submit the number to the datastore server
print "Player Data not found. Err201"
warn "Err201 - Value created and set to 1"
end
end)
game.Players.PlayerRemoving:Connect(function(plr) --when player is leaving
local function valueOfRep()
return plr.leaderstats.Reputation.Value
end
local success, err = pcall(function() --wrap in pcall
GameStatStorage:UpdateAsync(plr.UserId.."-Reputation", valueOfRep) --saves value the person has
end)
if success then
print "Save to leave Successful"
else
print("Error in saving to leave")
warn(err)
end
end)

3 comments
I tried this and changed line 23 to data + 10 so that if I existed in the store somehow as 0 it would give me 10 more than the last time I joined. This was not the case. Instead, every time I join I have 10 now. This is for sure a step forward though so I greatly appreciate the feedback!
No problem.
I got it to work by making the value of the fuction in line33 into a variable because in line38 it was a function in the parameters so by making it a variable I assume it parsed it and now it's saving values. Thank you so much for the help again! Couldn't have done it without you