Setting a bool value to true in datastore wont work

  1. What do you want to achieve? A ban gui for select people only.

  2. What is the issue? It wont set a BoolValue to true in a datastore

  3. What solutions have you tried so far? I have checked my code over and over yet havent found an issue and i get no error (and yes i have Access to API Services enabled)

local BP = game.ReplicatedStorage.BanPlayer
local DSS = game:GetService("DataStoreService")
local BDS = DSS:GetDataStore("BannedDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local BoolValue = Instance.new("BoolValue",plr)
	BoolValue.Name = "Banned"
	local Banned
	pcall(function()
		Banned = BDS:GetAsync("Banned-"..plr.UserId)
	end)
	
	if Banned ~= nil then
		BoolValue.Value = Banned
		if BoolValue.Value == true then
			plr:Kick("You are banned from this game!")
		end
	else
		BoolValue.Value = false
	end
	
end)

BP.OnServerEvent:Connect(function(player, playerToBan, reason)
	if playerToBan == "ROBLOX" then
		player:Kick("You can't just Ban the GUI Developer!")
	else
		game.Players[playerToBan].Banned.Value = true
		wait(.1)
		pcall(function()
			BDS:SetAsync("Banned-"..playerToBan.UserId, playerToBan.Banned.Value) -- This is not working. Everything else works but this ONE line!
		end)
		wait(5)
		game.Players:FindFirstChild(playerToBan):Kick(player.Name.." has Banned you from the game! Reason: "..reason)
	end

end)

If you know why it isn’t working i would like to know! I will also being trying stuff and looking at this

1 Like

Instead of storing Values inside the player, you could save a table with the UserId’s of the banned players using DataStore. Then, when a server starts you can GetAsync the table and check if the table has the players name and then do the Kicking.

I will try this give me a bit to set it up

1 Like

So turns out table datastores dont work how i thought it would
here is my code for checking it this also doesnt work as using a datastore editor plugin i added my UserId

	for i,v in pairs(Banned) do
		if Banned == plr.UserId then
			plr:Kick("You are banned from this game!")
		end
	end

and here is my code for adding a banned userid This may be the part wrong

		pcall(function()
			BDS:CreateAsync("Bans", playerToBan.UserId)
		end)

You have to use table.find(). You cannot check Banned = plr.UserId

Here is the article: table | Documentation - Roblox Creator Hub

Honestly im starting to think i should have never tried to make this ban system with datastore as i have rarely done anything with tables
Im using this code to try and find my UserId in the table but still nothing

	for i,v in pairs(Banned) do
		if Banned == table.find(BDS:GetAsync("Bans"),plr.UserId) then
			plr:Kick("You are banned from this game!")
		end
	end

First, use GetAsync to get the table and then use table.find()

Example

local dss = game:GetService("DataStoreService")
local banDS = dss:GetDataStore("Bans")

game.Players.PlayerAdded:Connect(function(plr)

local banTable = banDS:GetAsync(plr.UserId)

if banTable == nil then
local tablee = {} 
banDS:SetAsync(plr.UserId,tablee) 
elseif table.find(banTable,plr.UserId) then
plr:Kick("You have been banned!")
end
end)

You shouldn’t give up so easily, also you don’t necessarily need to store a table unless you need more data like Time till ban, who banned etc. If its just a banned/not banned value, then your bool method was fine, except the Bool inside the player seemed useless.

All you need to do in a simple logic flow:

  1. On Player Join Check if Player’s Ban datastore value exists or not, if yes then check if its true and kick accordingly, if no value exists means its a new / Unbanned player, so you don’t need to kick them.

  2. When some admin calls the remote event, you need to update the datastore for the Banned player, remember not the person who is banning, but the one is being banned and store if the person is banned or not.

  3. Last thing is, check if player is in game and kick them.

Bonus Feature: Use messaging Service so that you can ask all servers to check if that player is in the server and ban them, accordingly. This is pretty complex for you right now I believe, so I would recommend doing the others first.


Also a good habit is to use pcalls properly too while you work with Datastores, most people what they do is wrap a pcall around an anonymous function and do their datastore Calls inside the function, whereas it could just be done with something like this:

local key = player.UserId.."-Coins"
local success, response = pcall(CoinsDataStore.GetAsync, GetAsync, key)

if success then
   print(response) --Your data.
else
   warn("Error", response)
end

Well i plan to make an unban Gui that will involve UserId
also my script for the kicking worked fine it was just the fact that the datastore wouldn’t update

In your original script, are you sure UserId was a property of the playerToBan variable?

With the code it feels like you are passing in the Username of the player, if you’re passing a Username, you will need to find the player related to it and get their UserId, if its just UserId that you’re passing then just use the playerToBan directly, and it should work fine.

I used UserId but the data store would never update

You need to read my reply more carefully, I said that is the UserId a VALID property of the playerToBan variable?

Because I believe you are passing in a player Username, so basically UserId won’t be a valid property and therefore it won’t work. And the reason you don’t realise its not working because of that is because its wrapped in a pcall. If you try to print the response from the pcall you should get an error (I believe).

i feel like an idiot give me one second let me quickly do something

Alright well i have tried everything you guys have suggested but i need to head off to bed it is 3:30 AM where i am rn if anyone wants to try and still fix this here is my current model Admin Gui - Roblox