Datastore script help

Hello, I have these two scripts:

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

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.

2 Likes

image
it appeared that the player was removed, but he was not removed

1 Like

How do you know he wasn’t removed?

1 Like

I’m using a datastore plugin, it appears in the image I sent, the orange text is the players that are in the datastore

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’ve literally never used self-made datastores before in my life lol, but I’m going to try

Could you try just using :GetAsync to check?

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

image

That means that my script worked. Are you sure there isn’t a bug with your datastore plugin?

I’m going to write a script that prints the datastore every 5 seconds and I’ll see, I’ll be back

I’m not very familiar with datastores, so I think I’ll steer clear of that for now

So did my script work then?

No. The add part worked, the remove part didn’t work (I think, it might be a bug in my plugin)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.