How can I change the datastores of an offline user?

I don’t know how to convert the username into a user id for local data = bandata:GetAsync(game.Players:GetUserIdFromNameAsync(args[2]).. "-banned")

I also don’t know how to change the value of the offline player. Any help?

local datastore = game:GetService('DataStoreService')
local bandata = datastore:GetDataStore('Ban')


game.Players.PlayerAdded:Connect(function(plr)
	local banstats = Instance.new('Folder')
	banstats.Name = 'BanData'
	banstats.Parent = plr
	
	local bannedval = Instance.new('BoolValue')
	bannedval.Name = 'Banned'
	bannedval.Parent = banstats
	bannedval = false
	
	local data
	local scc, err = pcall(function()
		data = bandata:GetAsync(plr.UserId.."-banned")
	end)
	
	if data ~= nil then
		bannedval.Value = data
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local scc, err = pcall(function()
		bandata:SetAsync(plr.UserId.."-banned", plr.BanData.Banned.Value)
	end)
	
end)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if string.find(msg, "/ban") then
			local args = string.split(msg, " ")
			if game.Players:FindFirstChild(args[2]) then
				game.Players:FindFirstChild(args[2]).BanData.Banned.Value = true
				local plrname = game.Players:FindFirstChild(args[2])
				player.PlayerGui.Notification.Enabled = true
				player.PlayerGui.Notification.NotificationBox.NotificationText.Text = tostring(plrname).. ' was banned'
				wait(3)
				player.PlayerGui.Notification.Enabled = false
			else
				--local plrname = game.Players:FindFirstChild(args[2])
				local data = bandata:GetAsync(game.Players:GetUserIdFromNameAsync(args[2]).. "-banned")
				print(data)
				player.PlayerGui.Notification.Enabled = true
				player.PlayerGui.Notification.NotificationBox.NotificationText.Text = 'User doesnt exist'
				wait(3)
				player.PlayerGui.Notification.Enabled = false
			end			
		end
	end)
end)
1 Like

Offline player? What do you mean by that?

1 Like

I mean editing a offline players datastore

local id = game.Players:GetUserIdFromNameAsync(name)



Datastore:SetAsync(id.."-dataname")

You can get a players ID by their name, then edit data from their

Let me know if you have anymore questions

Where and how would I implement this into my script?

Also would this work with a offline player? I dont think it will work because the player isnt in the game

You have to replace

local data = bandata:GetAsync(game.Players:GetUserIdFromNameAsync(args[2]).."-banned")

with

local data = bandata:SetAsync(game.Players:GetUserIdFromNameAsync(args[2]).."-banned")

Replace :GetAsync with :SetAsync to set the players value to banned, not get it

I got an error that says argument 2 missing or nil

Like someone who is not playing the game currently?

Yes, that is how I want it to be

It does not matter then what status the player has, datastore works for anyone so you can just use your normal datastore scripts

When I set the value, I get an error that says argument 2 missing or nil for this script

local datastore = game:GetService('DataStoreService')
local bandata = datastore:GetDataStore('Ban')


game.Players.PlayerAdded:Connect(function(plr)
	local banstats = Instance.new('Folder')
	banstats.Name = 'BanData'
	banstats.Parent = plr
	
	local bannedval = Instance.new('BoolValue')
	bannedval.Name = 'Banned'
	bannedval.Parent = banstats
	bannedval = false
	
	local data
	local scc, err = pcall(function()
		data = bandata:GetAsync(plr.UserId.."-banned")
	end)
	
	if data ~= nil then
		bannedval.Value = data
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local scc, err = pcall(function()
		bandata:SetAsync(plr.UserId.."-banned", plr.BanData.Banned.Value)
	end)
	
end)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if string.find(msg, "/ban") then
			local args = string.split(msg, " ")
			if game.Players:FindFirstChild(args[2]) then
				game.Players:FindFirstChild(args[2]).BanData.Banned.Value = true
				local plrname = game.Players:FindFirstChild(args[2])
				player.PlayerGui.Notification.Enabled = true
				player.PlayerGui.Notification.NotificationBox.NotificationText.Text = tostring(plrname).. ' was banned'
				wait(3)
				player.PlayerGui.Notification.Enabled = false
			else
				--local plrname = game.Players:FindFirstChild(args[2])
				local data = bandata:SetAsync(game.Players:GetUserIdFromNameAsync(args[2]).. "-banned")
				print(data)
				player.PlayerGui.Notification.Enabled = true
				player.PlayerGui.Notification.NotificationBox.NotificationText.Text = 'User doesnt exist'
				wait(3)
				player.PlayerGui.Notification.Enabled = false
			end			
		end
	end)
end)

You wrote SetAsync instead of GetAsync (or failed to provide a new value to setasync)

Yes, I saw this issue and I fixed it, but I got an error called argument 2 missing or nil

Nvm, I think I found the issue, I forgot the 2nd value lol