How to change a player stats (a player that is not in game)

You can write your topic however you want, but you need to answer these questions:

  1. 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
  2. What is the issue? Include screenshots / videos if possible!
    I only know how to do it when player is in game
  3. 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.

2 Likes

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?

2 Likes

You should add a description - We don’t know exactly what you want.

You could mean changing a players datastore.

To do this, You would write that stat to a value of the players ID directly.

Hope I helped,
Lift_Surfer

1 Like

I use a datastore to store my data

1 Like

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

1 Like

Can you please show me a script example, because i dont only use intvalue i also use strinvalue for ranks

1 Like

It depends on how you’re storing user data. The example below uses the GlobalDataStore and assumes you store information using userId’s.

local userid = 12345
game:GetService("DataStoreService"):GetGlobalDataStore():SetAsync(userid, {Rank = "Admin"})
1 Like

What if i wanted to change the amount of coins? is it coins = coins + amount?

2 Likes

Sure, you can also do coins += amount.
If you meant you wanted to increase their coins in the DataStore, you can utilize IncrementAsync.

So, something like this?
game:GetService(“DataStoreService”):GetGlobalDataStore():SetAsync(userid, {Coins = Coins + 100})

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)

I am a storing:
Rank (stringvalue)
Coins (intvalue)
Prestige (intvalue)

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.

I want to be able to do it trhough a gui

Then make a GUI which fires an event to the server with parameters about the player and the stats of whom you want to change.

So on the serverside script i would use this
game:GetService(“DataStoreService”):GetGlobalDataStore():SetAsync(userid, {Coins = Coins + 100})

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)

I know its kinda confusing for a “beginner” scripter