Duplicate Detection Not Working?

So I’ve been trying to make a script that detects duplicate guild names, but for some reason, it won’t work, help?

local DatastoreService = game:GetService("DataStoreService")
local DS = DatastoreService:GetDataStore("GuildDatastore")
local guilds = {
	
}
local refreshtime = 30

game.ReplicatedStorage.Events.CreateGuild.OnServerEvent:Connect(function(plr, guildName)
	local data;
	local success, err = pcall(function()
		data = DS:GetAsync("GuildKey")
	end)
	if success then
		print("GuildData Sucessfully Loaded")
	else
		warn(err)
		script.Parent.RemoteEvent:FireClient(plr)
		return
	end
	if data then
		guilds = data.guildData
	end
	local dupe = false
	if guilds["guildName"] then
		dupe = true
	end
	if dupe == true then
		--show that name is dupe
	elseif dupe == false and plr.DaysSurvived.Value >= 20 then
		if plr.Gender.Value == "Female" then
			plr.Status.Value = "Duchess"
		elseif plr.Gender.Value == "Male" then
			plr.Status.Value = "Duke"
		else
			plr.Status.Value = "Duke"
		end
		plr.Guild.Value = guildName
		--give plr guild inviter
		table.insert(guilds,guildName)
		local data = {
			guildData = guilds
		}
		local success2, err2 = pcall(function()
			data = DS:SetAsync("GuildKey", data)
		end)
		if success2 and plr.leaderstats.Gols.Value >= 100 then
			print("GuildData Sucessfully Saved")
			plr.leaderstats.Gols.Value = plr.leaderstats.Gols.Value - 100
			plr.Character.Humanoid.DisplayName = plr.Status.Value.." "..plr.CharacterName.Value.." "..plr.Guild.Value
			script.Parent.Parent.Parent:Destroy()
		else
			warn(err2)
			script.Parent.RemoteEvent:FireClient(plr)
			return
		end
	end
	dupe = false
end)

You have a variable called guildName, but here you are looking if the string “guildName” is present in your guild-table (instead of looking for the actual guild name). Compare:

if guilds[guildName] then

This is incorrect, guilds is an dictionary with keys which are strings. Therefore you can either compare them as guilds["guideName"] or guilds.guideName.

Actually @Nonaz_jr is correct. guideName is a string that contains what needs to be found.

guilds["guideName"] would look for a member named “guideName” in the dictionary.

guilds.guideName would also look for a member named “guideName” in the dictionary.

guilds[guideName] would search for a member of guideNames value (i.e. if guideName = “The Guide” then it would search for guilds["The Guide"])

No, both of you’re incorrect, guilds["guideName"] is the same as guides.guideName, except that the latter is more efficient in memory and looks clean.

guilds[guideName] would only work if guideName isn’t a string or a number, for e.g:

local t = {

   [true] = 5,
   [workspace.Part] = 5

}

print(t[true]) 
print(t[workspace.Part])

This would be true if they were looking for an actual member name guideName.

Such as:

local t = {
  guideName = "Whatever"
}

But I don’t think this is the case here. Here, I believe they’re searching for something that guidName holds. For example, let guideName = "The Guide"

local t = {
   ["The Guide"] = "Whatever"
}

local guideName = "The Guide"
print (t[guideName])

This is the case here, I’m not going to argue pointlessly about this. Indexing a key will always get you it’s value. You would only use [""] if the key has a space in between, test this code for your self:

local t = {
   ["TheGuide"] = "Whatever", 
   ["A Space"] = 4,
   bobo = 50
}

print(t.TheGuide t["TheGuide"]) -- Whatever, Whatever
print(t["A Space"]) -- 4
print(t.bobo) -- 50
print(t["bobo"]) -- 50

Yes I understand those work, but the issue is you’re running for guideName, which does not exist in a table.

local t = {
	["TheGuide"] = "Whatever", 
	["A Space"] = 4
}

local guideName = "TheGuide"
print(t.guideName)
print(t["guideName"]) -- 4

Screen Shot 2021-03-21 at 12.04.01 AM

I completely understand your method, and it’s a valid method, but in this case guildName is not a member of the table Guild.

That isn’t my point. My point is that you can index keys with both [""] and directly assuming it has no spaces, even if it is an invalid key.

local t = {
  bo = 5
}

print(t["bo"], t.bo)

Correct. However, the issue in the main post is that guildName contains a string that needs to be found. So the author uses guild[“guildName”] which obviously will always be nil because there is no element of index “guildName” in the dictionary guild. Now, where Nonaz_jr was right about was correcting this to guild[guildName] which the compiler will replace to be (assuming guildName = “The Guild” as an example) guild["The Guild"]. I hope you can see why I pointed out that for this specific situation, your syntaxes wouldn’t work.

OP’s problem needs more context, hence why I made this post. I assumed that guildName would exist, anyhow, your point is valid here but do note that my point wasn’t invalid either, since it was about indexing keys via [""] or directly.

Yes that is true. I thought too that guildName was what had to be found but you can see further down the script that it is implicitly described as a string.

@njesk12 @SilentsReplacement So what should I do in conclusion?

You are passing in a argument guild name, assuming there should be no key with the value of guildName, therefore do:

if guilds[guildName] then
		dupe = true
	end
1 Like

it still does not work, I think there are some other errors in the script, I tested in-game, since I dont think datastore does not work on studio.

Make sure API services are enabled in order for Studio to access the data store API.

they are on, only the guild datastore doenst work

They still dont work? what can i do?

I tried to get help from discord, nothing helped much, is there a thing i can do?