Jump to content

[HELP] Script thinks empty table is same as not empty string


Kazafka

Recommended Posts

Wrote script 3 times, script thinks, that table "playersT", wich's empty, is the same as not empty string.

--Table
playersT = {}

--Code
addEventHandler("onPlayerConnect", root, function(nickname)
    for k, v in ipairs(playersT) do
    	if(nickname ~= v) then
        	--some code
        end
    end
end)

It's just keep not working. I RE-WROTE WHOLE SCRIPT 3 TIMES!

Edited by VaporZ
Link to comment
  • Moderators
3 minutes ago, VaporZ said:

But, I want to do something like, if connected player's nick isn't already in the table "playersT", then add the nickname.

--Table
playersT = {}

--Code
function isNicknameAlreadyAdded(nickname)
	for k, v in ipairs(playersT) do
		if v == nickname then
			return true -- found it, stop and return true
		end
	end
	return false -- nickname not found in playersT, return false
end

addEventHandler("onPlayerConnect", root, function(nickname)
    local alreadyAdded = isNicknameAlreadyAdded(nickname)
	
	if not alreadyAdded then
		-- some code
	end
end)

 

Link to comment

Here you have the whole function:

addEventHandler("onPlayerConnect", root, function(nickname)
	for k, v in ipairs(playersT) do
		if(nickname ~= v) then
			local size = #playersT
			local i = size + 1
			--
			playersT[i] = nickname --Add new player to bank
			moneyT[i] = "0" --Set new player's bank aco:O value to "0"
			--
			outputChatBox("#09ff00Your account has been added!", player, 255, 255, 255, true)
		end
	end
end)

 

Just now, stPatrick said:

--Table
playersT = {}

--Code
function isNicknameAlreadyAdded(nickname)
	for k, v in ipairs(playersT) do
		if v == nickname then
			return true -- found it, stop and return true
		end
	end
	return false -- nickname not found in playersT, return false
end

addEventHandler("onPlayerConnect", root, function(nickname)
    local alreadyAdded = isNicknameAlreadyAdded(nickname)
	
	if not alreadyAdded then
		-- some code
	end
end)

 

I'll try that

Oh yeah

That started working xD

4 minutes ago, stPatrick said:

--Table
playersT = {}

--Code
function isNicknameAlreadyAdded(nickname)
	for k, v in ipairs(playersT) do
		if v == nickname then
			return true -- found it, stop and return true
		end
	end
	return false -- nickname not found in playersT, return false
end

addEventHandler("onPlayerConnect", root, function(nickname)
    local alreadyAdded = isNicknameAlreadyAdded(nickname)
	
	if not alreadyAdded then
		-- some code
	end
end)

 

That started working thank you!

xD

TOPIC TO CLOSE!

Jesus, now I know, how small knowledge I have 'bout programming!

'gain, thanks!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...