• Function Always Returning True
    3 replies, posted
I have functions that check a MySQL database to see if a player is banned or exempt(meaning any addon restrictions don't apply to they player) from a specific action but regardless of the query they always seem to return true. [CODE] //Banned function RAS.IsBannedChat( steamid ) if !RAS.ValidSteamID( steamid ) then return false end Database:Query( "SELECT COUNT( * ) as isBanned FROM banned WHERE (steam_id = '"..steamid.."') AND (type = 'chat') AND (expire_time > '"..os.time().."')", function( result ) PrintTable(result) if result[1].data[1].isBanned == 1 then return true elseif result[1].data[1].isBanned == 0 then return false end end) end //Exempt function RAS.IsExemptChat( steamid ) if !RAS.ValidSteamID( steamid ) then return false end Database:Query( "SELECT COUNT( * ) as isExempt FROM exempt WHERE (steam_id = '"..steamid.."') AND (type = 'chat') AND (expire_time > '"..os.time().."')", function( result ) PrintTable(result) if result[1].data[1].isExempt == 1 then return true elseif result[1].data[1].isExempt == 0 then return false end end) end [/CODE] I have it setup for testing so that the IsBannedChat function will return a 1 from the query, so then I call the following function whenever a player chats: [CODE] //Called on player chat function RAS.Chat.Chatting( ply, text ) if RAS.Config.AntiSpamming then if !ply:CanChat() then if RAS.IsBannedChat( ply:SteamID() ) then RAS.ChatPrint( "Message \""..text.."\" was not sent because you are banned from chatting.\n", ply ) RAS.Log( ply:Nick().."["..ply:SteamID().."]".." tried to chat but is banned from chatting!" ) return false else RAS.ChatPrint( "Message \""..text.."\" was not sent.\n", ply ) RAS.ChatPrint( "STOP SPAMMING THE SERVER!", ply ) RAS.Log( ply:Nick().."["..ply:SteamID().."]".." was chat spamming the server!" ) return false end end ply:EnableChatting( false ) timer.Simple( RAS.Config.Chat.Delay, function() RAS.ChatResetTimer( ply ) end) end end hook.Add( "PlayerSay", "RAS.Chat.Chatting", RAS.Chat.Chatting ) //Check for a exempt, ban, spam function meta:CanChat() if !RAS.Config.AntiSpamming then return true elseif !RAS.Config.Chat.Enabled then return true elseif table.HasValue( RAS.Chat.Players, self:SteamID() ) then return false elseif RAS.IsExemptChat( self:SteamID() ) then return true elseif RAS.IsBannedChat( self:SteamID() ) then return false else return true end end [/CODE]
Database queries are asynchronous. You can't return a value for the RAS.IsBannedChat function from the callback because the callback executes after the function is run. Ideally, you should query the database on PlayerInitialSpawn and save the result in a variable on the player. Then, in your function, just check if that variable exists on the player and return it if it does.
[QUOTE=AK to Spray;50193294]helpful words[/QUOTE] So something like this? [code] hook.Add( "PlayerAuthed", "RASSetChatBanned", function( ply ) Database:Query( "SELECT COUNT( * ) as isExempt FROM exempt WHERE (steam_id = '"..RAS.GetSteamID( ply ).."') AND (type = 'chat') AND (expire_time > '"..os.time().."')", function( result ) if result[1].data[1].isExempt == 1 then ply.RASChatBanned = true elseif result[1].data[1].isExempt == 0 then ply.RASChatBanned = false end end) end) //Check isBanned function RAS.IsBannedChat( steamid ) if !RAS.ValidSteamID( steamid ) then return false end local player.GetBySteamID( steamid ) = ply if ply.RASChatExempt = true then return true else return false end end [/code]
[QUOTE=MexicanR;50193486]So something like this? [code] hook.Add( "PlayerAuthed", "RASSetChatBanned", function( ply ) Database:Query( "SELECT COUNT( * ) as isExempt FROM exempt WHERE (steam_id = '"..RAS.GetSteamID( ply ).."') AND (type = 'chat') AND (expire_time > '"..os.time().."')", function( result ) if result[1].data[1].isExempt == 1 then ply.RASChatBanned = true elseif result[1].data[1].isExempt == 0 then ply.RASChatBanned = false end end) end) //Check isBanned function RAS.IsBannedChat( steamid ) if !RAS.ValidSteamID( steamid ) then return false end local player.GetBySteamID( steamid ) = ply if ply.RASChatExempt = true then return true else return false end end [/code][/QUOTE] 1. You are using = to check for equal values. You need to use == 2. You don't need to set the variable to false. 3. You don't need to explicitly return true or false or compare values with true or false. [lua]function RAS.IsBannedChat( steamid ) if !RAS.ValidSteamID( steamid ) then return end local player.GetBySteamID( steamid ) = ply return IsValid(ply) and ply.RASChatBanned end function RAS.IsBannedChat( steamid ) if !RAS.ValidSteamID( steamid ) then return false end local player.GetBySteamID( steamid ) = ply return IsValid(ply) and ply.RASChatBanned == true end[/lua] Either of those would work fine.
Sorry, you need to Log In to post a reply to this thread.