DataStore Not working

Hello, so I am working on some testing and my script to set the ban to true is not working for some reason only works very few times out of many, how do I fix it? here is the code

local DataStore = game:GetService("DataStoreService"):GetDataStore("BannedDataStore")

game.ReplicatedStorage.BanTesting.OnServerEvent:Connect(function(player, Text)

local player = game.Players:GetUserIdFromNameAsync(Text)

print(player)

DataStore:SetAsync(player, true)

end)

why do you have to redefine player again? hey i think you’re trying to get the person’s (who you’re trying to ban) name, why don’t you rename local player to local banTarget or something? also, it’s most likely that you are sending in too many datastore requests, have you checked the output?

i just put it here just to clean up the whitespace

local DataStore = game:GetService("DataStoreService"):GetDataStore("BannedDataStore")

game.ReplicatedStorage.BanTesting.OnServerEvent:Connect(function(player, Text)
    local banTarget = game.Players:GetUserIdFromNameAsync(Text)
    print(banTarget.Name)
    DataStore:SetAsync(banTarget, true)
end)

I check output everytime I test and I would be aware of it was too many datastore request as ive had that problem before, so that isnt the problem

can you try adding print(banTarget.name)?

It will 100% not work your way you removed the convert name to user id as I need that for the key. as the key requires user id

i didn’t remove the :GetUserIdFromNameAsync and either way, you can just do banTarget.UserId

Datastores don’t save on a per player basis. They take in keys(string) that will allow access to the datastore. Here is a fixed version:

local DataStore = game:GetService("DataStoreService"):GetDataStore("BannedDataStore")

game.ReplicatedStorage.BanTesting.OnServerEvent:Connect(function(player, userid) -- Presuming this is a userid

print(userid)

DataStore:SetAsync("bans_"..tostring(userid), true)

end)

oh, i forgot about that lol, maybe change userid back to text and then define a variable for the userid?

local DataStore = game:GetService("DataStoreService"):GetDataStore("BannedDataStore")

game.ReplicatedStorage.BanTesting.OnServerEvent:Connect(function(player, Text)
    local banTarget = game.Players:GetUserIdFromNameAsync(Text)
    local userid = banTarget.UserId
    DataStore:SetAsync("bans_"..tostring(userid), true)
end)

But on part that checks if the player is banned, here it is:

local DataStore = game:GetService("DataStoreService"):GetDataStore("BannedDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local banStatus = DataStore:GetAsync("bans_"..tostring(plr.UserId))
	if banStatus then
		plr:Kick("You have been banned.")
	end
end)
2 Likes

Wait where do I put that and where do I put the other script, here is the other part of the ban script then you tell me what I need to put where because i’m so confused rn

local DataStoreService = game:GetService("DataStoreService");

local DataStore = DataStoreService:GetDataStore("BannedDataStore");

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

local boolval = Instance.new("BoolValue", player)

local variable = DataStore:GetAsync(player.UserId)

print(variable)

boolval.Value = DataStore:GetAsync(player.UserId) or false

if boolval.Value == true then

player:Kick("Banned.")

end

end)

game.Players.PlayerRemoving:Connect(function(player)

DataStore:SetAsync(player.UserId, player:FindFirstChild("Value").Value)

end)

Replace your old code completely with the new code. Make sure this is a server script in the ServerScriptService.

You sent me 2 codes which do I use?

The first code I sent is the script that actually bans the player.

The second one is the code that checks if the player is banned then kicks them.

1 Like

Try to use different variables, and also don’t get the player from inside the game, just use the UserId.

local DSS = game:GetService("DataStoreService")
local BanDS = DSS:GetDataStore("BannedDataStore")

game.ReplicatedStorage.BanTesting.OnServerEvent:Connect(function(Player, UserId)
BanDS:SetAsync(UserId, true)
end)

I’d also recommend putting the save in a pcall to prevent script errors.
Take a look at this good article by @ReturnedTrue Pcalls - When and how to use them

1 Like

Didn’t work, this is the code

local DataStore = game:GetService("DataStoreService"):GetDataStore("BannedDataStore")

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

local banStatus = DataStore:GetAsync("bans_"..tostring(plr.UserId))

if banStatus then

plr:Kick("You have been banned.")

end

end)

game.ReplicatedStorage.BanTesting.OnServerEvent:Connect(function(player, userid) -- Presuming this is a userid

print(userid)

DataStore:SetAsync("bans_"..tostring(userid), true)

end)

Yeah I know how to use pcalls, very helpful they are.

did you send a username instead of a userid to the RemoteEvent?

I forgot to check output it printed my username, it looks so funny since it says userid.Userid now lol

I printed what it said and now it is showing up as nil here is the code


game.Players.PlayerAdded:Connect(function(plr)
	local banStatus = DataStore:GetAsync("bans_"..tostring(plr.UserId))
	if banStatus then
		plr:Kick("You have been banned.")
	end
end)
game.ReplicatedStorage.BanTesting.OnServerEvent:Connect(function(player, userid) -- Presuming this is a userid
	local userId2 = userid.UserId
	print(userId2)
	DataStore:SetAsync("bans_"..tostring(userId2), true)
end)

you sent a username, right? you gotta use :GetPlayerFromTextAsync() to get the actual player object

game.Players.PlayerAdded:Connect(function(plr)
	local banStatus = DataStore:GetAsync("bans_"..tostring(plr.UserId))
	if banStatus then
		plr:Kick("You have been banned.")
	end
end)

game.ReplicatedStorage.BanTesting.OnServerEvent:Connect(function(player, userid) -- Presuming this is a userid
    local player = game.Players:GetPlayerFromTextAsync(userid)
	local userId2 = player.UserId
	print(userId2)
	DataStore:SetAsync("bans_"..tostring(userId2), true)
end)