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 4 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:

--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)


1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 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:

--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.Name = "leaderstats" --names it
    folder.Parent = plr --places it 

    local reputation = Instance.new("IntValue") -- makes a place to store the value of the players reputaion
    reputation.Name = "Reputation"
    reputation.Parent = folder --places the value container


    local data

    local success, err = pcall(function() --wraps in pcall
        data = GameStatStorage:GetAsync(plr.UserId.."-Reputation") --gets the players data
    end)
    if success then --if good then 
        print "Successfully Loaded" --Success message
       reputation.Value = data
    else --or if player doesnt have data to pull
        plr.leaderstats.Reputation.Value = 1 -- then give them a value of zero to display
    warn(err)
    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:SetAsync(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)


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 — 4y
0
No problem. youtubemasterWOW 2741 — 4y
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 — 4y
Ad

Answer this question