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:
01 | --Datastore-- |
02 | local Datastore = game:GetService( "DataStoreService" ) --gets service |
03 | local GameStatStorage = Datastore:GetDataStore( "gamestats" ) --creates a link to use for accessing the datastore |
04 |
05 |
06 | game.Players.PlayerAdded:Connect( function (plr) --when a player joins |
07 | local folder = Instance.new( "Folder" ) --Makes folder for stats |
08 | folder.Parent = plr --places it |
09 | folder.Name = "leaderstats" --names it |
10 | local reputation = Instance.new( "IntValue" ) -- makes a place to store the value of the players reputaion |
11 | reputation.Parent = folder --places the value container |
12 | reputation.Name = "Reputation" |
13 | local success, err = pcall ( function () --wraps in pcall |
14 | GameStatStorage:GetAsync(plr.UserId.. "-Reputation" ) --gets the players data |
15 | end ) |
Hello, snoppyploptart. You wrote reputation.Value = success
. This would not work because success is a boolean and the value is an integer. Instead, add a variable and set it to the GameStatStorage:GetAsync()
function as it returns the data, in this case, a number. Also, you should use :SetAsync()
instead of :UpdateAsync()
and it is recommended to set the instance's name first and then parent it while using Instance.new()
.. Try this:
01 | --Datastore-- |
02 | local Datastore = game:GetService( "DataStoreService" ) --gets service |
03 | local GameStatStorage = Datastore:GetDataStore( "gamestats" ) --creates a link to use for accessing the datastore |
04 |
05 |
06 | game.Players.PlayerAdded:Connect( function (plr) --when a player joins |
07 | local folder = Instance.new( "Folder" ) --Makes folder for stats |
08 | folder.Name = "leaderstats" --names it |
09 | folder.Parent = plr --places it |
10 |
11 | local reputation = Instance.new( "IntValue" ) -- makes a place to store the value of the players reputaion |
12 | reputation.Name = "Reputation" |
13 | reputation.Parent = folder --places the value container |
14 |
15 |
If it doesn't work, make sure API Services are enabled. Note that DataStores don't work in studio. If this helped you, make sure to accept the answer!