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)
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)
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)
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)
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)
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)