Check object action exists (Lua Script)

  • #1, by darren-beckettTuesday, 15. September 2015, 15:05 9 years ago
    Can somebody please fix my script?

    if game.CurrentObject.ObjectActions[DoubleClick]:isEmpty() then
    print("No DoubleClick Action")
    end

    Error: attempt to index a nil value

    Great Poster

    384 Posts


  • #2, by afrlmeTuesday, 15. September 2015, 15:57 9 years ago
    hmm... you forgot to wrap the name of the object action in "these", like so... "Double click".

    Imperator

    7285 Posts

  • #3, by darren-beckettTuesday, 15. September 2015, 20:33 9 years ago
    No, it says
    Error: DoubleClick could not be found in linklist

    Great Poster

    384 Posts

  • #4, by sebastianTuesday, 15. September 2015, 20:36 9 years ago
    erm yeah... what do you want to check? If there is a Double Click Action, but empty or If there is a Double Click Action at all?

    Thread Captain

    2346 Posts

  • #5, by afrlmeTuesday, 15. September 2015, 20:53 9 years ago
    I'm guessing they want to check if the action exists or not so they can call another action maybe?

    It's not DoubleClick, it's Double click. Table & field names are case sensitive. They have to be written exactly as they are inside of the editor.

    Example: I have a command called left_click, now to access the action for that inside of the current object below the cursor I could do something like...
    if game.CurrentObject.ObjectActions["'left_click' executed"] ~= nil then
     print("exists")
    else
     print("does not exist")
    end
    

    ... I have no idea if what I've just written is correct though as I'm writing off the top of my head.

    Imperator

    7285 Posts

  • #6, by darren-beckettTuesday, 15. September 2015, 23:10 9 years ago
    Still not working.
    I have a DoubleClick action and the object below the cursor does not have a DoubleClick action assigned (It only has a LeftClick). Whichever way I write the code, it returns nil or could not be found in linklist.

    Great Poster

    384 Posts

  • #7, by sebastianTuesday, 15. September 2015, 23:46 9 years ago
    thats not an action you have there. these are all buttons

    Thread Captain

    2346 Posts

  • #8, by darren-beckettTuesday, 15. September 2015, 23:56 9 years ago
    These are what I select as actions for my objects.
    The objects all have actions for when the LeftClick or DoubleClick is Executeted on it.

    Great Poster

    384 Posts

  • #9, by afrlmeWednesday, 16. September 2015, 00:17 9 years ago
    Look at my example script above.

    If you are talking about accessing an executed action inside of an object then it would be: "'DoubleClick' executed".

    * edit: I completely forgot about the command checker script I wrote ages ago. Might serve as a useful basis but not as it currently is...

    local act = game.CurrentObject.ObjectActions
    
    -- iterate through the object list with a for loop...
    for i = 1, #act do
     if i < #act then if act[i]:getName() == "'DoubleClick' executed" then
      print("DoubleClick exists"); break
     elseif i == #act and act[i]:getName() ~= "'DoubleClick' executed" then
      print("DoubleClick does not exist")
     end
    end
    

    Imperator

    7285 Posts

  • #10, by darren-beckettWednesday, 16. September 2015, 13:53 9 years ago
    Thanks for your help, I've now created a function i can use globally:

    function objectActionExists(obj, actionName)
    local strActionName = "'" .. actionName .. "' executed"
    local act = obj.ObjectActions
    
    	-- iterate through the object list with a for loop...
    	for i = 1, #act do
    		if act[i]:getName() == strActionName then
    			return true
    		end
    	end
    	return false
    end
    

    Great Poster

    384 Posts

  • #11, by LebosteinWednesday, 16. September 2015, 13:56 9 years ago

    Key Killer

    621 Posts