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

Incorect value is being uploaded to database, how to fix that?

Asked by 4 years ago

Hey, I have a small issue. To keep this short, there is a local value inside that get's it's a value from "leaderstats" and uses it to upload it to an external database. And everything would be fine except for one thing. The uploaded value is the same as the downloaded from the database when the player joined the game, even if the value within the leaderstats was changed, the uploaded value is not changed. Example: User joins the game, the value downloaded from the database & has been set to the leaderstats is 150 points. The user buys something for 130 points, and the value is now 20 points. The user leaves the game and the value that is uploaded to the database is 150.

How may I make the correct value to be uploaded to the database? Thanks for any help!

local HttpService = game:GetService("HttpService")
local player = game:GetService("Players")
local numberValue 
MilesAmount = player:WaitForChild("leaderstats"):WaitForChild("Miles")


local FirebaseService = require(game:GetService("ServerScriptService"):WaitForChild('FirebaseService'))
local database = FirebaseService:GetFirebase("Miles");
local Players = game:GetService("Players");



game.Players.PlayerAdded:Connect(function(player)
    local GetData = database:GetAsync(player.UserId)
    if GetData == nil then
        print('User has no saved data!')
    elseif GetData ~= nil then 
        local RAW = HttpService:JSONEncode(GetData)
        print('Data Found!'..RAW)
        local numberValue = tonumber(RAW:match("[%.%d]+"))
        print (numberValue)

        local leaderstats = Instance.new("Folder")
        leaderstats.Name = "leaderstats"
        leaderstats.Parent = player

        local Miles = Instance.new("IntValue")
        Miles.Name = "Miles"
        Miles.Parent = leaderstats

        if numberValue == nil then
            Miles.Value = 0
        elseif numberValue ~= nil then
            Miles.Value = numberValue
        else
            warn("Unknown error!")
        end
    end
end)

MilesAmount.Changed:Conenct(function()
    local SendData = player.leaderstats.Miles.Value
end)

game.Players.PlayerRemoving:Connect(function(player)
    local SendData = player.leaderstats.Miles.Value
    print (SendData) -- Just for bug debuggin
    database:SetAsync(player.UserId, game.HttpService:JSONEncode(SendData))
    print('Data should be saved.')
end)

Answer this question