Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Leaderboard not updating/saving values?

Asked by 5 years ago

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--
02local Datastore = game:GetService("DataStoreService") --gets service
03local GameStatStorage = Datastore:GetDataStore("gamestats") --creates a link to use for accessing the datastore
04 
05 
06game.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)
View all 43 lines...

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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--
02local Datastore = game:GetService("DataStoreService") --gets service
03local GameStatStorage = Datastore:GetDataStore("gamestats") --creates a link to use for accessing the datastore
04 
05 
06game.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 
View all 46 lines...

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!

0
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! snoppyploptart 59 — 5y
0
No problem. youtubemasterWOW 2741 — 5y
0
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 snoppyploptart 59 — 5y
Ad

Answer this question