Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my code not find the password in a table?

I have a script that's supposed to get password input from a remote event (works fine) and if the password matches one from the table it gets the place id of the password and returns a value true or false depending on if the password was right.

Example:

	re:FireClient(plr,false) -- password wasn't correct
	re:FireClient(plr,true) -- password was correct

Any help is appreciated!

My code is here:

local TpService = game:GetService("TeleportService")

local re = game.ReplicatedStorage["HelloScripters!!"]

local passwords = {}
-- Passwords --
passwords["12 15 05"] = {}
passwords["15 15 25"] = {}

-- Password place IDs --
passwords["12 15 05"].id = "8346845291"
passwords["15 15 25"].id = "2548213281"

print(passwords)

local function tpTo(ID,PLAYER)
	TpService:Teleport(ID,PLAYER)
end

re.OnServerEvent:Connect(function(plr,password)
	print("Received password input by "..plr.Name..". Entered password: "..password)
	
	local success = false
	
	local brr
	
	local thing = table.find(passwords,password) -- doesnt find it and returns nill even if the password exists
	
	print(thing)
	
	--[[
	for i,thing in pairs(passwords) do
		if thing.Name == password then
			brr = thing
		end 
	end
	]]
	if brr then
		local id = brr.id
		if id then
			success = true
			print("Info: Password & ID found! Password: "..brr.." ID: "..id)

		else
			print("DANGER: No ID for password: "..brr)
		end
	else
		print("Warning: Incorrect or non-existing password.")
	end
	
	
	
	
	--[[
	for i, bruh in ipairs(passwords) do
		if bruh == password then
			success = true
			print(bruh)
			print(bruh.id)
			re:FireClient(plr,true)
			tpTo(bruh.id,plr)
		end
	end
	]]
	
	if success == false then
		re:FireClient(plr,false)
	else
		re:FindClient(plr,true)
	end
end)

1 answer

✓ Accepted answer

The reason why it's not finding the password is because you have the password set to the index, not the value. The table.find function is trying to find a value inside the passwords table and because there is no value of "12 15 05" and "15 15 25" it is returning nil.

To fix this, you don't really need to use a for loop in this case, you can just do if passwords[password] then ... this will return the value of the index, which you can use to determine if it's there or not.

Also, I recommend using a RemoteFunction if you need to return a value back, a RemoteFunction will wait for a response, in this case, it would be whether this was successful or not.

4 comments

0

there is a value of "12 15 05" and "15 15 25" heres the table: https://imgur.com/a/xO9MREv

0

there is a value of "12 15 05" and "15 15 25" heres the table: https://imgur.com/a/xO9MREv

0

ok nvm it works idk why lol but thanks

0

Again, they aren't the values, they are the indexes. If you look at a table, every value has an index, whether you are assigning one yourself or it's automatically assigned to a number. In this case, you are setting your own index of "12 15 05" and setting the value of it to the table which has "id" stored inside of.

This discussion is preserved as it was. New answers, comments, and votes are closed.