You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to be able to change a player’s stats when they are not in the game
What is the issue? Include screenshots / videos if possible!
I only know how to do it when player is in game
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- This is an example Lua code block
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
Hey there, we really need more info to help you here. Player stats could be stored in millions of different ways. How is your data currently being stored?
If you are using datastoreservice not datastore2 then you can simply Get the player’s key of the datastore if you are eg using userid stats-45011362 would be a datastore for me in this case, which you can Get and set values to in that way. If it’s datastore2 you can’t edit players stats without the player instance
What kind of stats are you storing? If you’re only doing coins then you should only be storing an integer.
If you’re still going with the table idea, you can do:
local datastore = game:GetService(“DataStoreService”):GetGlobalDataStore()
local userid = 12345
datastore:UpdateAsync(userid, function(info)
info.Coins += 100
return info
end)
Otherwise, assuming you only store coins, you may use IncrementAsync like this:
local datastore = game:GetService(“DataStoreService”):GetGlobalDataStore()
local userid = 12345
datastore:IncrementAsync(userid, 100)
Then store your player data in a dictionary with 3 fields. If you want to increment their coins, use the portion of code which I wrote that uses UpdateAsync.
Well, If I use that then how am I going to store rank data since its a string value and it only accept intvalue, also I want to be able to change a players rank, coins in gui if they are or not in game
You said your “Rank” is a StringValue; if you want to change someone’s stats while they are not ingame then you run that code in Studio (assuming you have API access enabled) or in any server in your game using the developer console. If you want the changes to replicate to their GUI then refresh/change the leaderstats Value.
Again, if you were to use SetAsync to change something dependant on its old value (which is what UpdateAsync is for), you should store the old stats first. You also have to assume that they have stats in the first place, so please do not consider using the code below because I am only writing this since you for some reason insist on only using SetAsync while the other information that I told you seems to fly over your head.
local datastore = game:GetService(“DataStoreService”):GetGlobalDataStore()
local old = datastore:GetAsync(userid)
old.Coins += 100
datastore:SetAsync(userid, old)