So Im creating a script where you can send crates/skins to anyone if their offline or online. I need help trying to do it. I’m conflicted because I don’t know how to edit the leaderstats or any datastore if their offline. Example jailbreak you can send people crates if their offline.
You can edit a player’s DataStore regardless of if they’re online or not.
for instance if you’re not online and I need to save something to your DataStore, I would do this if you’re using normal DataStores:
-- for instance I need to modify a value 'Cash' saved to your UserId
-- let your user Id be 111 , I intend on setting your Cash value to 10
local DataStoreService = game:GetService("DataStoreService")
local store = DataStoreService:GetDataStore("Cash")
local success, err = pcall(function()
store:SetAsync(111, 10)
end)
if not success then
error("data could not be saved")
end
Of course this is just an example and you could try error prevention etc.
Note that there is a difference between when to use DataStore:SetAsync() and
DataStore:UpdateAsync() , the latter considers previous values too.
It really depends on how you handle datastores to give an accurate response.
I will write this in the perspective of having a data cache of players who are ingame.
If you save data by Player.UserId
(which is highly recommended) you can achieve this easily.
Say I have a remote event where a player submits a name, and a crate to give.
-- just some psuedocode, not all practices here are good
GiveCrate.OnServerEvent:Connect(function(sender, targetName, crateType)
-- this is an example, you could have your datastore module here and whatnot.
-- assume that PlayerDataStore is our DataStore object for players (:GetDataStore("PlayerData"))
-- also assume that sessionData is our current data session
local Players = game.Players
local success, targetUserId = pcall(Players.GetUserIdFromNameAsync, Players, targetName)
if success then
local senderData = sessionData[sender.Name] -- i use .Name because names won't change ingame (edge case..)
local targetData = PlayerDataStore:GetAsync(targetUserId)
if senderData and targetData then
-- you can subtract from senderData.Crates and add to targetData.Crates here, or whatever.
if senderData.Crates[crateType] > 0 then
senderData.Crates[crateType] = senderData.Crates[crateType] - 1
targetData.Crates[crateType] = targetData.Crates[crateType] + 1
PlayerDataStore:SetAsync(targetUserId, targetData)
end
end
else
-- tell the client somewhere we couldn't do it
warn("failed to get user id: " .. targetUserId)
end
end)
Keep in mind this assumes the other player is offline. If they’re in a different server you’ll need a different solution.
As mentioned above this is just an example, I don’t know how your datastore systems are set up, you should pcall
more of these gets and sets to DataStores, etc. I hope this does put you on the right track though.