Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Quick question with datastores. How can I access them even if a player is not in game?

Asked by 5 years ago

I'm making a banning/unbanning system and data editing system, but how could I make a player/userid data store edit? what kind of datastore would I need to use? an ordered datastore?

0
use a different key User#23365 30 — 5y
0
:GetAsync(key) User#24403 69 — 5y
0
:GetAsync(key) User#24403 69 — 5y

2 answers

Log in to vote
1
Answered by
LawlR 182
5 years ago
Edited 5 years ago

Just a normal datastore. However, the player will need to visit the place first in order for it to work. You also need to make sure that Enable Studio Access to API Services is ticked on the game's page.

local DataStore = game:GetService("DataStoreService"):GetDataStore("YourDataStore")
local UserId = game.Players:GetUserIdFromNameAsync("NameOfUserWhosStatYouWantToChange")
local key = ("P" .. UserId) -- Whatever your key is.

for i,v in pairs(DataStore:GetAsync(key)) do 
    print(DataStore:GetAsync(key)[i])  -- Gets all of their stats.
end
-- To know what all the values are, just look at where you're saving them, and look at the order. The first one to print is the first one you're saving.

DataStore:SetAsync(key, true) -- Example, would set banned to true if your first value is the "Banned" value.

-- You can also do maths on their stats.

local First = (DataStore:GetAsync(key)[1])
DataStore:SetAsync(key, First + 10) -- Would add 10 to their value. Must be an IntValue.

For more information: https://developer.roblox.com/articles/Data-store

1
wow it's really that simple? I never thought of it in this way thank you! VeryDarkDev 47 — 5y
Ad
Log in to vote
0
Answered by
dionant 23
5 years ago

Quoting the article on Datastores (https://developer.roblox.com/articles/Data-store) :

local DataStoreService = game:GetService("DataStoreService") 

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

local success, err = pcall(function()
    experienceStore:SetAsync("Player_1234", 50)
end)

That script basically gets the DataStoreService, then gets the data store called "PlayerExperience"

You can set values such as :SetAsync(Player, Ban) where ban would be a bool value.

You can use GetAsync(Player) and it would return the Ban value.

local DataStoreService = game:GetService("DataStoreService") 

local Banlist = DataStoreService:GetDataStore("Banlist")


function BanPlayer(Player)
local success, err = pcall(function()
    Banlist:SetAsync(Player, true)
end)
--kick the player from the game
end

function UnbanPlayer(Player)
local success, err = pcall(function()
    Banlist:SetAsync(Player, false)
end)
end

function CheckBan(Player)

local success, Banned = pcall(function()
    return Banlist:GetAsync(Player)
end)

if success then
  --if Banned then kick the player in here.
end

end

Basically, Ban and Unban functions basically go to the Player value within Banlist and set it to true or false. CheckBan would check if a player is banned or not.

The script I made here isn't the best but it's to give you the concept. You should always use the Player's userId and NOT the username, since usernames can be changed, but UserIDs never change. Basically, something like BanPlayer(userId)

I'm not the best at scripting but I hope i've helped you.

I suggest you visit this article I quoted at first.

Answer this question