Cant Access offline Players Datastore

I have this script for admin commands:

-- this script is for trolling players by jumpscaring and doing other things in their server
-- Also for kicking and banning them from the game

--Troll command : /e troll {username} {name image}
-- Kick command: /kick {username} {reason}
-- Kick reason must be a singular word or have the words seperated by "_" for example "Stop_Exploiting"
-- Ban command: /ban {username} {reason}

local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")


local message = game:GetService("MessagingService")
local admins = {"talis783"}

local commands = {"kick", "ban", "troll", "unban"}

local commandsFunction = {
	["/troll"] = function(player, image)
		for i,v in pairs(game:GetService("Players"):GetChildren()) do
			if v.Name:lower() == player:lower() then
				game.ReplicatedStorage.Troll:FireClient(v,image)
			end
		end
	end,
	["/kick"] = function(player, reason)
		game.Players:FindFirstChild(player):Kick(reason)
	end,
	["/ban"] = function(player, reason)
		game.Players:FindFirstChild(player):FindFirstChild("ToolsSave"):FindFirstChild("BannedCheck").Value = true
		task.wait(3)
		print(game.Players:FindFirstChild(player):FindFirstChild("ToolsSave"):FindFirstChild("BannedCheck").Value)
		game.Players:FindFirstChild(player):Kick("You have been banned for reason: "..reason)
		
	end,
	["/unban"] = function(player, DontNeedThis)
		local playerId = game.Players:GetUserIdFromNameAsync(player)

		local playerData
		local success, error = pcall(function()
			playerData = playerDataStore:GetAsync(tostring(playerId))
		end)

		if success then
			-- Modify the BannedCheck value to false
			if playerData and playerData[3] ~= nil then
				playerData[3] = false

				-- Save the modified data back to the data store
				local saveSuccess, saveError = pcall(function()
					playerDataStore:SetAsync(tostring(playerId), playerData)
				end)

				if saveSuccess then
					print("Player data saved successfully.")
				else
					warn("Failed to save the player's data: " .. saveError)
				end
			else
				warn("Player data not found or BannedCheck value is nil.")
			end
		else
			warn("Failed to access the player's data store: " .. error)
		end
	
	end,
}


game.Players.PlayerAdded:Connect(function(plr)
	if table.find(admins,plr.Name) then
		print("Found plr in [admins] list")
		plr.Chatted:Connect(function(plrmessage)
			plrmessage = plrmessage:lower():split(" ")
			
			for i = 1, #commands do
				
				if plrmessage[1]:match(commands[i]) then
					
					local func = commandsFunction[plrmessage[1]]
					
					func(plrmessage[2], plrmessage[3])
			
				end
	
			end
		
		end)
	end
end)

and this one for my DataStore


local DataStore = game:GetService("DataStoreService")
local PlrDataStore = DataStore:GetDataStore("PlayerData")

local admins = {"talis783"}

game.Players.PlayerAdded:Connect(function(Player)
	
	local ToolsSave = Instance.new("Folder")
	ToolsSave.Parent = Player
	ToolsSave.Name = "ToolsSave"

	local SnowGlobe = Instance.new("StringValue")
	SnowGlobe.Parent = ToolsSave
	SnowGlobe.Name = "SnowGlobe"
	
	
	local Flashlight = Instance.new("StringValue")
	Flashlight.Parent = ToolsSave
	Flashlight.Name = "Flashlight"
	
	local BannedCheck = Instance.new("BoolValue")
	BannedCheck.Parent = ToolsSave
	BannedCheck.Name = "BannedCheck"
	BannedCheck.Value = false
	
	
	local PlayerData

	local succes, ErrorMsg = pcall(function()
		local PlayerId = Player.UserId
		PlayerData = PlrDataStore:GetAsync(PlayerId)
	end)
	
	
	if succes then
		if PlayerData then
			BannedCheck.Value = PlayerData[3]
			print(BannedCheck.Value)
			if BannedCheck.Value == true then
				--if not table.find(admins, Player.Name) then
				task.wait(10)
					Player:Kick("You are banned")
				--end
				
			end
		end

	else
		warn(ErrorMsg)
	end
end)


local function saveData(Player)
	local ToolsSave = Player.ToolsSave
	local PlayerData = {ToolsSave.SnowGlobe.Name, ToolsSave.Flashlight.Name, ToolsSave.BannedCheck.Value}
	
	
	local Success, ErrorMsg = pcall(function()
		PlrDataStore:SetAsync(Player.UserId, PlayerData)
	end)

	if not Success then
		warn(ErrorMsg)
	end

	if Success then
	end
end

game.Players.PlayerRemoving:Connect(function(Player)
	saveData(Player)
end)

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		saveData(Player)
	end
end)

Everything works except the ability to unban someone. I banned myself just to test it and gave myself 10 seconds to unban myself. However the datastore script still thinks i am banned

5 Likes

Explain what you are trying to access.

The Datastore of a player who is offline. A bool value called “BannedCheck”

Blockquote
playerData = playerDataStore:GetAsync(tostring(playerId))

Well, in your Datastore, the access key is a number not a ‘string’.

I dont know what that means, I only need to be able to change the value to false

Found it, on your first script, you set the retrieved data, not the player’s physical ‘BannedCheck’ instance.

and how would i fix that, char limit

Replace this IF statement:

if playerData and playerData[3] ~= nil then
				playerData[3] = false

				-- Save the modified data back to the data store
				local saveSuccess, saveError = pcall(function()
					playerDataStore:SetAsync(tostring(playerId), playerData)
				end)

				if saveSuccess then
					print("Player data saved successfully.")
				else
					warn("Failed to save the player's data: " .. saveError)
				end
			else
				warn("Player data not found or BannedCheck value is nil.")
			end

to:

				player.BannedCheck.Value = false

				-- Save the modified data back to the data store
				local saveSuccess, saveError = pcall(function()
					playerDataStore:SetAsync(tostring(playerId), playerData)
				end)

				if saveSuccess then
					print("Player data saved successfully.")
				else
					warn("Failed to save the player's data: " .. saveError)
				end
			else
				warn("Player data not found or BannedCheck value is nil.")
			end
["/unban"] = function(player, DontNeedThis)
		local playerId = game.Players:GetUserIdFromNameAsync(player)

		local playerData
		local success, error = pcall(function()
			playerData = playerDataStore:GetAsync(tostring(playerId))
		end)

		if success then
			-- Modify the BannedCheck value to false
			player.BannedCheck.Value = false

			-- Save the modified data back to the data store
			local saveSuccess, saveError = pcall(function()
				playerDataStore:SetAsync(tostring(playerId), playerData)
			end)

			if saveSuccess then
				print("Player data saved successfully.")
			else
				warn("Failed to save the player's data: " .. saveError)
			end
		else
			warn("Player data not found or BannedCheck value is nil.")
		end
	end,

heres the new code and datastore still says im banned

Have you tried unbanning you yet? Like RIGHT now?

yes I run the command to unban myself and it does not work

First of all, are you unbanning yourself with an actual account in - game?
Second of all, before unbanning yourself in - game, publish the changes.

apparently the /unban command im typing in chat does not trigger the function, let me see if i can fix that

fixed it, the command is triggering but im still not being unbanned

ServerScriptService.Admin:48: attempt to index nil with ‘Value’

["/unban"] = function(player, DontNeedThis)
	print("unban command")
	
	local playerId = game.Players:GetUserIdFromNameAsync(player)

	local playerData
	local success, error = pcall(function()
		playerData = playerDataStore:GetAsync(tostring(playerId))
	end)

	if success then
		-- Modify the BannedCheck value to false
		player.BannedCheck.Value = false

		-- Save the modified data back to the data store
		local saveSuccess, saveError = pcall(function()
			playerDataStore:SetAsync(tostring(playerId), playerData)
		end)

		if saveSuccess then
			print("Player data saved successfully.")
		else
			warn("Failed to save the player's data: " .. saveError)
		end
	else
		warn("Player data not found or BannedCheck value is nil.")
	end
end,

Read this.
And unban yourself with another admin. (Preferrably an alt.)

yes im unbanning myself ingame with my own account

What line is 48? I need information for what line it is please.

player.BannedCheck.Value = false

is the line

player:WaitForChild("BannedCheck").Value = false