Jump to content

'nil' = no data or space used, right?


koragg

Recommended Posts

I decided to make a nickname locking script today for my server and it all works perfectly except a very small typo bug that I can't fix (I don't know why, I've fixed it this way before and there were no issues). So below is the script and the problem is that if there is no nickname saved to my account and I type /protected it says "Your currently protected nick is nil." in chat while it should say the appropriate line in the 'else' condition which is "You haven't protected any nickname yet.". That's at lines 49-60. Any idea why it still thinks there is account data when I already set it to 'nil'? And I thought that if I want to remove data and free the space used by it, I should indeed use 'nil' so dunno what's wrong with this script. It has worked a lot of times before.

local nickTimer
local joinTimer
-------------------------------------------------------------------------------------------------------------------------
function lockNick(player, command, name)
	local nick
	local account = getPlayerAccount(player)
	if isGuestAccount(account) then
		outputChatBox("You need to register and login in order to protect a nickname!", player, 255, 153, 0, true)
		return
	end
	if not name then
		nick = getPlayerName(player):gsub('#%x%x%x%x%x%x', '')
		setAccountData(account, "protectedNick", nick)
	else
		nick = name:gsub('#%x%x%x%x%x%x', '')
		setAccountData(account, "protectedNick", nick)
	end
	outputChatBox("You successfully protected nickname #FFFFFF"..nick.." #00FF00to your account.", player, 0, 255, 0, true)
end
addCommandHandler("protect", lockNick)
-------------------------------------------------------------------------------------------------------------------------
function unlockNick(player, command, name)
	local nick
	local account = getPlayerAccount(player)
	if isGuestAccount(account) then
		outputChatBox("You need to register and login in order to unprotect a nickname!", player, 255, 153, 0, true)
		return
	end
	if not name then
		nick = getPlayerName(player):gsub('#%x%x%x%x%x%x', '')
		if nick == getAccountData(account, "protectedNick"):gsub('#%x%x%x%x%x%x', '') then
			setAccountData(account, "protectedNick", nil)
			outputChatBox("You successfully unprotected nickname #FFFFFF"..nick.." #00FF00from your account.", player, 0, 255, 0, true)
		else
			outputChatBox("No such nickname found in the database.", player, 255, 0, 0, true)
		end
	else
		nick = name:gsub('#%x%x%x%x%x%x', '')
		if nick == getAccountData(account, "protectedNick"):gsub('#%x%x%x%x%x%x', '') then
			setAccountData(account, "protectedNick", nil)
			outputChatBox("You successfully unprotected nickname #FFFFFF"..nick.." #00FF00from your account.", player, 0, 255, 0, true)
		else
			outputChatBox("No such nickname found in the database.", player, 255, 0, 0, true)
		end
	end
end
addCommandHandler("unprotect", unlockNick)
-------------------------------------------------------------------------------------------------------------------------
function checkProtectedNicks(player)
	local account = getPlayerAccount(player)
	if isGuestAccount(account) then
		return
	end
	if getAccountData(account, "protectedNick") then
		outputChatBox("#32CD32Your currently protected nick is #FFFFFF"..getAccountData(account, "protectedNick").."#32CD32.", player, 255, 255, 255, true)
	else
		outputChatBox("#CD5C5CYou haven't protected any nickname yet.", player, 255, 255, 255, true)
	end
end
addCommandHandler("protected", checkProtectedNicks)
-------------------------------------------------------------------------------------------------------------------------
function onNickChange(oldNick, newNick)
	local account = getPlayerAccount(source)
	local allAccounts = getAccounts()
	for k, v in ipairs(allAccounts) do
		if v ~= account then
			if getAccountData(v, "protectedNick") and newNick:gsub('#%x%x%x%x%x%x', '') == getAccountData(v, "protectedNick"):gsub('#%x%x%x%x%x%x', '') then
				setTimer(function(player)
					changeNickToRandom(player)
					outputChatBox("#CD5C5CYour nick was changed to a random one because you used a protected nick.", player, 255, 255, 255, true)
				end, 1000, 1, source)
			elseif getAccountData(v, "protectedNick") and newNick:gsub('#%x%x%x%x%x%x', '') ~= getAccountData(v, "protectedNick"):gsub('#%x%x%x%x%x%x', '') then
				if isTimer(nickTimer) then
					killTimer(nickTimer)
				end
			end
		end
	end
	if getAccountData(account, "protectedNick") and newNick:gsub('#%x%x%x%x%x%x', '') == getAccountData(account, "protectedNick"):gsub('#%x%x%x%x%x%x', '') then
		if isTimer(nickTimer) then
			killTimer(nickTimer)
		end
		outputChatBox("#32CD32You are allowed to use this protected nick.", source, 255, 255, 255, true)
	end
end
addEventHandler("onPlayerChangeNick", root, onNickChange)
-------------------------------------------------------------------------------------------------------------------------
function onPlayerJoin()
	joinTimer = setTimer(function(player)
		local account = getPlayerAccount(player)
		local allAccounts = getAccounts()
		local currentNick = getPlayerName(player):gsub('#%x%x%x%x%x%x', '')
		if not isGuestAccount(account) then
			local nickData = getAccountData(account, "protectedNick"):gsub('#%x%x%x%x%x%x', '')
			if currentNick == nickData then
				outputChatBox("#32CD32You are now allowed to use this protected nick.", player, 255, 255, 255, true)
				return
			end
		else
			for k, v in ipairs(allAccounts) do
				if getAccountData(v, "protectedNick") and currentNick == getAccountData(v, "protectedNick"):gsub('#%x%x%x%x%x%x', '') then
					outputChatBox("#CD5C5CYour nick #FFFFFF"..getAccountData(v, "protectedNick"):gsub('#%x%x%x%x%x%x', '').." #CD5C5Cis protected. Please login or change nick within 60 seconds.", player, 255, 255, 255, true)
					if isTimer(nickTimer) then
						killTimer(nickTimer)
					end
					nickTimer = setTimer(function(player)
						changeNickToRandom(player)
						outputChatBox("#CD5C5CYour nick was changed to a random one because you used a protected nick.", player, 255, 255, 255, true)
					end, 60000, 1, player)
				end
			end
		end
	end, 10000, 1, source)
end
addEventHandler("onPlayerJoin", root, onPlayerJoin)
-------------------------------------------------------------------------------------------------------------------------
function onPlayerLogin()
	local account = getPlayerAccount(source)
	local currentNick = getPlayerName(source):gsub('#%x%x%x%x%x%x', '')
	local nickData = getAccountData(account, "protectedNick"):gsub('#%x%x%x%x%x%x', '')
	local lockedNick
	local allAccounts = getAccounts()
	for k, v in ipairs(allAccounts) do
		if v ~= account then
			lockedNick = getAccountData(v, "protectedNick")
		end
	end
	if nickData and currentNick == nickData then
		if isTimer(nickTimer) then
			killTimer(nickTimer)
		end
		if isTimer(joinTimer) then
			return
		end
		outputChatBox("#32CD32You are now allowed to use this protected nick.", source, 255, 255, 255, true)
		return
	elseif lockedNick and currentNick == lockedNick then
		changeNickToRandom(source)
		outputChatBox("#CD5C5CYour nick was changed to a random one because you used a protected nick.", source, 255, 255, 255, true)
	end
end
addEventHandler("onPlayerLogin", root, onPlayerLogin)
-------------------------------------------------------------------------------------------------------------------------
function onPlayerLogout()
	local account = getPlayerAccount(source)
	local curName = getPlayerName(source):gsub('#%x%x%x%x%x%x', '')
	local allAccounts = getAccounts()
	if isGuestAccount(account) then
		for k, v in ipairs(allAccounts) do
			if getAccountData(v, "protectedNick") and curName == getAccountData(v, "protectedNick") then
				changeNickToRandom(source)
				outputChatBox("#CD5C5CYour nick was changed to a random one because you used a protected nick.", source, 255, 255, 255, true)
			end
		end
	end
end
addEventHandler("onPlayerLogout", root, onPlayerLogout)
-------------------------------------------------------------------------------------------------------------------------
function changeNickToRandom(player)
	local randomNick = "Guest"..math.random(100000000,999999999)
	while getPlayerFromName(randomNick) do
		randomNick = "Guest"..math.random(100000000,999999999)
	end
	setPlayerName(player, randomNick)
end
-------------------------------------------------------------------------------------------------------------------------
function adminRemoveNick(player, command, name)
	local account = getPlayerAccount(player)
	local accountName = getAccountName(account)
	if isGuestAccount(account) then
		return
	end
	if isObjectInACLGroup("user."..accountName, aclGetGroup("Admin")) or isObjectInACLGroup("user."..accountName, aclGetGroup("SuperModerator")) then
		if not name then
			outputChatBox("#FF0000Correct command is : #FEFE22/freenick [full nick]", player, 0, 255, 0, true)
			return
		end
		local allAccounts = getAccounts()
		for k, v in ipairs(allAccounts) do
			if getAccountData(v, "protectedNick") then
				if string.lower(getAccountData(v, "protectedNick"):gsub('#%x%x%x%x%x%x', '')) == string.lower(name:gsub('#%x%x%x%x%x%x', '')) then
					outputChatBox("#32CD32You successfully unlocked nick #FFFFFF"..getAccountData(v, "protectedNick").."#32CD32.", player, 255, 255, 255, true)
					setAccountData(v, "protectedNick", nil)
				else
					outputChatBox("No such nickname is saved in the database.", player, 255, 0, 0, true)
				end
			end
		end
	end
end
addCommandHandler("freenick", adminRemoveNick)

 

Edited by koragg
Link to comment

Well on the wiki it says

value: The value you wish to store. Set to false to remove the data. NOTE: you cannot store tables as values, but you can use toJSON strings.

Try to set it to false to remove the data instead of setting it to nil and see. Setting it to nil and expecting it to be removed is indeed logical, as far as I know setting setElementData to nil does actually remove it.

Edited by pa3ck
  • Like 1
Link to comment
45 minutes ago, koragg said:

Yea well I'm doing everything with account data because i can't use sql xD too hard

You already script in LUA, using SQL is waaaay easier. You should try to learn MySQL, believe me when I say it is not hard. You only need to understand couple of SQL commands like SELECT, INSERT, UPDATE, DELETE and that's basically it, if you know these, you know SQL. You don't need to create tables with commands, you can just use phpMyAdmin. Have you looked at any tutorials? I learnt SQL on w3schools, but if there are no proper tutorials on the MTA forums, I will make a tutorial video myself this weekend.

Link to comment
5 hours ago, pa3ck said:

You already script in LUA, using SQL is waaaay easier. You should try to learn MySQL, believe me when I say it is not hard. You only need to understand couple of SQL commands like SELECT, INSERT, UPDATE, DELETE and that's basically it, if you know these, you know SQL. You don't need to create tables with commands, you can just use phpMyAdmin. Have you looked at any tutorials? I learnt SQL on w3schools, but if there are no proper tutorials on the MTA forums, I will make a tutorial video myself this weekend.

I haven't looked at any tutorials as my first encounter with the executeSQLQuery was a big disaster. I needed a cash script for my server so i downloaded "mwrpanel". It saved cash to player nickname, not account. You can guess how useless that was so i asked a friend how to fix it and he said with account data. Since then i make everything at my server with account data and convert sql scripts to use account data instead (i had a sql nick protection but made this^ myself so that it uses account data and i know 100% how it works). 

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...