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

1if value == true then
2return nil
3else
4--do purchase etc
5end
6--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:

01moneye.Event:connect(function(cost, plr, key)
02    if cost <= playerLeaderstats[plr]["Cash"] then
03    playerLeaderstats[plr]["Cash"] = playerLeaderstats[plr]["Cash"] - cost
04    cashe:Fire(playerLeaderstats[plr]["Cash"])
05    local user = "User_" .. plr.userId
06    local saves = Characters:GetAsync(user)
07    Characters:UpdateAsync(saves[1], function(oldval)
08        local newval = true
09        return newval
10    end)
11    print(saves[1], "leaderboard update ???")
12    return nil
13    else
14        return print("Not Enough")
15        end
16    end)

Button/Event Trigger-er:

01script.Parent.MouseButton1Click:connect(function()
02    if saves[1] == false then
03    local tofire = plr:FindFirstChild("Events").MoneyTransfer
04    tofire:Fire(COST, plr, saves[1])
05    --local Char1 = true
06    --local Chars = {Char1, }
07    --Characters:UpdateAsync(user, Chars)
08    print(saves[1], "< true?")
09    else
10        return nil
11    end
12 
13end)

Datastore Join:

01--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.
02 
03local DataStoreService = game:GetService('DataStoreService')
04local Characters = DataStoreService:GetDataStore('CharacterSaves')
05 
06game.Players.PlayerAdded:connect(function(player)
07local user = "User_" .. player.userId
08local saves = Characters:GetAsync(user)
09 
10if saves then
11    Char1 = saves[1]
12    print(Char1, "1")
13    Char2 = saves[2]
14    print(Char2, "2")
15    Char3 = saves[3]
View all 39 lines...

Datastore Leave:

01local DataStoreService = game:GetService('DataStoreService')
02local Characters = DataStoreService:GetDataStore('CharacterSaves')
03 
04game.Players.PlayerRemoving:connect(function(player)
05local user = "User_" .. player.userId
06local saves = Characters:GetAsync(user)
07 
08local Char1 = saves[1]
09local Char2 = saves[2]
10local Char3 = saves[3]
11local Char4 = saves[4]
12local Char5 = saves[5]
13local Char6 = saves[6]
14local Char7 = saves[7]
15 
View all 23 lines...

Anything else needed let me know.

1 answer

Log in to vote
1
Answered by 9 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.

01local history= {}
02--on player joined event
03history[player.Name] = {}
04--add all purchase data from datastore to the list
05--all data should be strings
06 
07--leaderboard
08--you should propably add something that has the name or Id of the item purchased (as a string)
09moneye.Event:connect(function(cost, plr, key, id) --add new string id/name
10 if cost <= playerLeaderstats[plr]["Cash"] then
11    for _, v in pairs(history[player.Name])do
12        if(v==id)then return end --id isthe id of the product or item (can also be name)
13    end
14    playerLeaderstats[plr]["Cash"] = playerLeaderstats[plr]["Cash"] - cost
15    table.insert(history[player.Name], id) --id is the id or name of the item
View all 26 lines...

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 — 9y
Ad

Answer this question