DataStore Not working

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)

I’m sorry w h a t, that’s not a valid function of the Players service

game.Players.PlayerAdded:Connect(function(plr)
	local banStatus = DataStore:GetAsync("bans_"..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)

Also there are a couple of mistypo/capitalization errors

plr.UserId returns back a string value, no need to convert it into a string

it isn’t? OP’s post made me think it was lol, i guess use game.Players:FindFirstChild(playerName)

this would probably work:

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

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

So use this code because none of these have worked and i’m extra confused

i mean, it’s worth a try to test it out and i don’t feel like getting on studio to try it out

1 Like

Wait this code works so nvm my last saying, letme mark you as solution

1 Like

oh i forgot, remember to use pcall() so no errors occur

Side note, you should check if there’s at least a valid banTarget in the game, otherwise it’ll return back as nil if the Player enters a incorrect name

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

game.ReplicatedStorage.BanTesting.OnServerEvent:Connect(function(player, playerName) 
    local banTarget = game.Players:FindFirstChild(playerName)
    print(banTarget)

    if banTarget then
        local userid = banTarget.UserId
        print(userid)
	    DataStore:SetAsync("bans_"..userid, true)
    end
end)
3 Likes