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:
This is incorrect, guilds is an dictionary with keys which are strings. Therefore you can either compare them as guilds["guideName"] or guilds.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:
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.