Script 1 (add player to datastore) - this one works perfectly.
local DataStoreService = game:GetService("DataStoreService")
local WhitelistDataStore = DataStoreService:GetDataStore("DataStoreName")
game:GetService("Players").PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
local command, username = string.match(message, "^%-whitelist%s+(%a+)%s+(%w+)$")
if command == "add" and username then
local success, error = pcall(function()
local dataKey = "Whitelist_" .. username
WhitelistDataStore:SetAsync(dataKey, true)
print("Player " .. username .. " added to whitelist.")
end)
if not success then
print("Error: " .. error)
end
end
end)
end)
Script 2 (remove the player from the datastore) - this does not work, the player is simply not removed from the datastore.
local DataStoreService = game:GetService("DataStoreService")
local WhitelistDataStore = DataStoreService:GetDataStore("DataStoreName")
game:GetService("Players").PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
local command, username = string.match(message, "^%-whitelist%s+(%a+)%s+(%w+)$")
if command == "remove" and username then
local success, error = pcall(function()
local dataKey = "Whitelist_" .. username
WhitelistDataStore:RemoveAsync(dataKey)
print("Player " .. username .. " removed from whitelist.")
end)
if not success then
print("Error: " .. error)
end
end
end)
end)
You should be using UserIds instead of user names, because if the user changes their name, their whitelist will be removed, and you’re allowing others who take their name to be in the whitelist too. You should also be putting both of these code sections into 1 script.
Code:
--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local WhitelistDataStore = DataStoreService:GetDataStore("DataStoreName")
--//Tables
local cache = {}
--//Functions
local function getUserIdFromName(name)
local userIdFromCache = cache[name]
if userIdFromCache then
return userIdFromCache
end
local userId = nil
local success, _ = pcall(function()
userId = Players:GetUserIdFromNameAsync(name)
end)
if success then
cache[name] = userId
return userId
end
warn("Unable to find UserId for Name:", name)
return nil
end
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
local command, username = string.match(message, "^%-whitelist%s+(%a+)%s+(%w+)$")
if not username then
return
end
local userId = getUserIdFromName(username)
if not userId then
return
end
print(username, userId)
if command == "add" then
local success, errorMessage = pcall(function()
WhitelistDataStore:SetAsync("Whitelist_".. userId, true)
end)
if success then
print("Player " .. username .. " added to whitelist.")
else
print("Error: " .. errorMessage)
end
elseif command == "remove" then
local success, errorMessage = pcall(function()
WhitelistDataStore:RemoveAsync("Whitelist_".. userId)
end)
if success then
print("Player " .. username .. " removed from whitelist.")
else
print("Error: " .. errorMessage)
end
end
end)
end)
If the above doesn’t work, tell me what it prints.
this doesn’t help fix ur script, but I suggest you check out profileservice by berezaa, its simple to use and has a bunch of stuff built in to make it secure and run well. its infinitely better than default datastore and i personally think its way better and easier to use than datastore 2
I don’t know if you meant that, but if so, here it is
local DataStoreService = game:GetService("DataStoreService")
local DataStoreName = "DataStoreName"
local dataStore = DataStoreService:GetDataStore(DataStoreName)
local playerKey = "Whitelist_485680559"
local success, data = pcall(function()
return dataStore:GetAsync(playerKey)
end)
if success then
if data then
print(playerKey .. ": " .. tostring(data))
else
print(playerKey.." not found")
end
end