DataStore Not working

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

I can wrap it in a pcall, I like to do work too haha

2 Likes