Solved Attempt to call method

Discussion in 'Rust Development' started by Taffy, Oct 17, 2014.

  1. Hi,

    Looking at changing the welcome message on my oxide 2 plugin so that it only appears when the wakeup button is pressed. Mughisi suggested that I look at the wakeup console command as it can be intercepted with the OnRunCommand which is a good shout.

    So I copied the code I was using from the OnPlayerSpawn function and get the following error on the server when the user clicks the "wakeup" button -
    [Oxide] 7:56 PM [Error] usernotification: [string "usernotification.lua"]:42: at
    tempt to call method 'ChatMessage' (a string value)

    Any ideas?

    The code from the OnPlayerSpawn function:
    Code:
    function PLUGIN:OnPlayerSpawn( ply,connection )
        if(self.Config.ShowConnected) then
            tmpuser= ( connection.username )
                -- Send UserMessage
                usermsg = (self.Config.UserMessage)
                usermsg = usermsg:gsub("USERNAME",tmpuser)
                ply:ChatMessage(usermsg)  -- sends the message to the connecting player
                    --Send AllUserMessage
                    allusermsg = (self.Config.AllUserMessage)
                    allusermsg = allusermsg:gsub("USERNAME",tmpuser)
                    global.ConsoleSystem.Broadcast("chat.add \"" .. self.Config.ChatName .. "\" \"" .. allusermsg .. "\"")
        end
    end
    
    That was working fine but the welcome message would make more sense when the wakeup button is pressed. The below code is from the OnRunCommand function which is producing the error when the wake up button is pressed.

    Code:
    function PLUGIN:OnRunCommand( ply )
        usermsg = (self.Config.UserMessage)
        usermsg = usermsg:gsub("USERNAME",tmpuser)
        if (ply.wakeup) then
            ply:ChatMessage(usermsg)  -- sends the message to the connecting player
            print (usermsg)
        end
    end
    The print(usermsg) command does produce the desired results to the console.

    Cheers
    Dave
     
  2. from wulf command block:
    Code:
    function PLUGIN:OnRunCommand(arg, wantsfeedback)
        -- Sanity checks
        if (not arg) then return end
        if (not arg.connection) then return end
        if (not arg.connection.player) then return end
        if (not arg.cmd) then return end
        if (not arg.cmd.name) then return end    -- Create friendly variables
        local player = arg.connection.player
        local command = arg.cmd.name
        local blocked = false    -- Loop through blocked commands
        for key, setting in pairs(self.Config.Settings.Commands) do
            -- Check against list of blocked commands
            if (string.match(setting, "^(" .. command .. ")$")) then
                blocked = true
            end        -- Check if blocked command was found
            if (blocked) then
                -- Check if sent from user
                if (player) then
                    -- Send command blocked message to user via chat
                    player:SendConsoleCommand("chat.add \"" .. self.Config.Settings.ChatName .. "\" \"" .. self.Config.Messages.CommandBlocked .. "\"")
                    -- Send command blocked message to user via F1 console
                    player:SendConsoleCommand("echo " .. self.Config.Messages.CommandBlocked)
                end            -- Debug messages
                if (debug) then
                    print("[" .. self.Title .. "] Player tried to run console command \"" .. arg.cmd.name .. "\" but was blocked by configuration")
                end            return false -- Disable the command
            end
        end
    end
    
     
  3. Wulf

    Wulf Community Admin

    ply:ChatMessage should still work fine still.
     
  4. Yeah not quiet sure why its not tbh. Replaced with player:SendConsoleCommand and added some of the sanity checks which seems to work. Suddenly dawned on me this might not be a good idea. Everytime the user dies and click wake up button they are going to get the welcome message. Looking to see if I can establish how long the user has been connected with a view to not run the send if connection > say 1 minute. Still looking :p
     
  5. You could store a boolean for each user when recieved the welcome message and check for the bool before sending again.