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

Datastore Troubles: UpdateAsync or SetAsync?

Asked by 8 years ago

So, I'm having troubles creating keys (multiple keys for the object being requested to be pruchased) to the User's ID, I've done something similar to this before but used a boolean value as the key then upon leaving saved it if it changed and used:

if value == true then
return nil
else
--do purchase etc
end
--However, a player could change the value to true easily making the game pointless to play, it defeats the purpose if you can cheat playing the game by changing it's value with a simple: false to true.

, instead I want to store the value inside the script so it's theoretically impossible to change it without using a script. Currently I have a table value called "Cash" this value is stored in a script so it can't be changed, when the player clicks the button (Event Trigger-er) they fire a BindableEvent created inside their Player to request the leaderboard to calculate if they can buy this thing. However, they can still buy it again if they have enough money even though they just unlocked it, what I want is to check the datastore to see if the key for the object is true if so, don't fire the event, the reason for it is because again, a player can change a boolean's value easily. Any help is appreciated, if more code is needed I can provide it, also, I don't really expect a giant mass of code, so if you don't want to create it you don't have to, I'm good with an explanation.

Leaderboard:

moneye.Event:connect(function(cost, plr, key)
    if cost <= playerLeaderstats[plr]["Cash"] then
    playerLeaderstats[plr]["Cash"] = playerLeaderstats[plr]["Cash"] - cost
    cashe:Fire(playerLeaderstats[plr]["Cash"])
    local user = "User_" .. plr.userId
    local saves = Characters:GetAsync(user)
    Characters:UpdateAsync(saves[1], function(oldval)
        local newval = true
        return newval
    end)
    print(saves[1], "leaderboard update ???")
    return nil
    else
        return print("Not Enough")
        end
    end)

Button/Event Trigger-er:

script.Parent.MouseButton1Click:connect(function()
    if saves[1] == false then
    local tofire = plr:FindFirstChild("Events").MoneyTransfer
    tofire:Fire(COST, plr, saves[1])
    --local Char1 = true
    --local Chars = {Char1, }
    --Characters:UpdateAsync(user, Chars)
    print(saves[1], "< true?")
    else
        return nil
    end

end)

Datastore Join:

--Note, these are all values delcared when the player FIRST, joins the game as seen in the else and below, what I want to do is update them to the server so they can't trigger the event for that value again. Then saves upon leaving.

local DataStoreService = game:GetService('DataStoreService')
local Characters = DataStoreService:GetDataStore('CharacterSaves')

game.Players.PlayerAdded:connect(function(player)
local user = "User_" .. player.userId
local saves = Characters:GetAsync(user)

if saves then
    Char1 = saves[1]
    print(Char1, "1")
    Char2 = saves[2]
    print(Char2, "2")
    Char3 = saves[3]
    print(Char3, "3")
    Char4 = saves[4]
    print(Char4, "4")
    Char5 = saves[5]
    print(Char5, "5")
    Char6 = saves[6]
    print(Char6, "6")
    Char7 = saves[7]
    print(Char7, "7")

else
    local Char1v = false
    local Char2v = false
    local Char3v = false
    local Char4v = false
    local Char5v = false
    local Char6v = false
    local Char7v = false
    local Chars = {Char1v, Char2v, Char3v, Char4v, Char5v, Char6v, Char7v}

    Characters:SetAsync(user, Chars)    
end

end)

Datastore Leave:

local DataStoreService = game:GetService('DataStoreService')
local Characters = DataStoreService:GetDataStore('CharacterSaves')

game.Players.PlayerRemoving:connect(function(player)
local user = "User_" .. player.userId
local saves = Characters:GetAsync(user)

local Char1 = saves[1]
local Char2 = saves[2]
local Char3 = saves[3]
local Char4 = saves[4]
local Char5 = saves[5]
local Char6 = saves[6]
local Char7 = saves[7]


local Chars = { Char1, Char2, Char3, Char4, Char5, Char6, Char7 }

Characters:SetAsync(user, Chars)



end)

Anything else needed let me know.

1 answer

Log in to vote
1
Answered by 8 years ago

If I were you, I would not make request to the server to fetch data that you can keep within the server. Simply make a table with purchases and if that table contains a purchased item deny the purchase.

local history= {}
--on player joined event
history[player.Name] = {}
--add all purchase data from datastore to the list
--all data should be strings

--leaderboard
--you should propably add something that has the name or Id of the item purchased (as a string)
moneye.Event:connect(function(cost, plr, key, id) --add new string id/name
 if cost <= playerLeaderstats[plr]["Cash"] then
    for _, v in pairs(history[player.Name])do
        if(v==id)then return end --id isthe id of the product or item (can also be name)
    end
    playerLeaderstats[plr]["Cash"] = playerLeaderstats[plr]["Cash"] - cost
    table.insert(history[player.Name], id) --id is the id or name of the item
    cashe:Fire(playerLeaderstats[plr]["Cash"])
    local user = "User_" .. plr.userId
    local saves = Characters:GetAsync(user)
    --you should really save all purchased data in a table as strings
    Characters:SetAsync(saves[1], history[player.Name] )
    print(saves[1], "leaderboard update ???")
    return nil
    else
        return print("Not Enough")
        end
    end)

I'm not entirely sure if that works or is anything useful but hopefully it provided some insight on how to rewrite your code.

0
Thanks, I'll test this out today, yesterday was very busy, I'll let you know if it worked out. Evadable 65 — 8y
Ad

Answer this question