I originally took this to ROBLOX's SH but no-one's suggestions seemed to help.
What I am trying to do is create a player level which currently increments when you click script.Parent, a brick with a ClickDetector.
Also, it works in Solo Mode. Nothing else.
01 | local datastore = game:GetService( "DataStoreService" ):GetDataStore( "PlayerLevel" ) |
02 |
03 | wait( 10 ) --Too lazy to check for character properly |
04 |
05 | script.Parent.ClickDetector.MouseClick:connect( function (plr) |
06 | local key = "user_" ..plr.userId |
07 | local gui = plr.PlayerGui.MainUI.plrLevelGui.PlrLvlNum |
08 | datastore:IncrementAsync(key, 1 ) |
09 | gui.Text = datastore:GetAsync(key) |
10 | end ) |
Here's how the wiki demonstrates use of IncrementAsync.
1 | local number = DataStore:IncrementAsync(key, 1 ) |
That would increase the DataStore's value by one, and also return the value of the new value, since GetAsync doesn't yield immediately after IncrementAsync is used. Here's a fixed version of your script:
01 | local datastore = game:GetService( "DataStoreService" ):GetDataStore( "PlayerLevel" ) |
02 |
03 | wait( 10 ) |
04 |
05 | script.Parent.ClickDetector.MouseClick:connect( function (plr) |
06 | local key = "user_" ..plr.userId |
07 | local gui = plr.PlayerGui.MainUI.plrLevelGui.PlrLvlNum |
08 | local newValue = datastore:IncrementAsync(key, 1 ) |
09 | gui.Text = tostring (newValue) |
10 | end ) |
This was taken from the wiki and edited to fit your needs. If there's any problems, comment. If this works, upvote!