Roblox - Making a custom event to detect mouse clicks? - lua

Firstly, I do not want to use the Mouse object, so MouseButton1Click is not an option. I want to use UserInputService, but my solution still seems unclean.
ModuleScript:
function GuiModule.Click(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
return true
else
return false
end
end
LocalScript:
local GuiModule = require(game.Players.LocalPlayer.PlayerGui.GuiModule)
Button.InputEnded:Connect(function(Input)
if GuiModule.Click(Input) then --I really have to write an if statement for every InputEnded event?
print("Button clicked")
end
end)
Ideally, I want something like this, where .Clicked is a custom event that utilizes object-oriented programming in a module script. I've looked into metatables and metafunctions but I can't fully grasp their functionality to implement this, if its possible.
local GuiModule = require(game.Players.LocalPlayer.PlayerGui.GuiModule)
Button.Clicked:Connect(function(Input)
print("Button clicked")
end)

Try the Activated event instead. It automatically handles different input types so you don't have to detect whether it's mouse or touch input.
Button.Activated:Connect(function()
print("Button clicked or tapped")
end)

Related

LuaU script (Roblox), how can I make a Key get pressed with a script

An example would have been like
local E = game:GetService('UserInputService').SetKeyDown(Enum.KeyCode.E)
but it doesnt work ofcourse because i cant jsut make my game press E by itself with this thing, so it requieres soemthing longer and if you find the solution can you also do one where it makes it pressed down?
The input can only be registered on the client, therefore you will have to code in a LocalScript. There are 2 services which are used to get the player's input:-
UserInputService
This example shows how to use the UserInputService in getting the player's LeftMouseButton input.
local UserInputService = game:GetService("UserInputService")
local function onInputBegan(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed!")
end
end
UserInputService.InputBegan:Connect(onInputBegan)
ContextActionService
This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function ()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
You should take a look at the Wiki pages.
It sounds like you want to write a script to press the E key, but that isn't possible.
You can bind actions to keypresses, just like in the examples Giant427 provided, and you can also manually call the functions that are bound to those actions, but you cannot write a script to fire keyboard inputs.

Making a Clothing GUI 2021 (Roblox Studio)

I Am Trying to make a GUI that gives you an outfit when you Click it (Text Button). Its Currently not Working. This is the Script I used.
script.Parent.MouseButton1Click:FindFirstChild(function(WhoClick)
local WhoClick = game.Players.LocalPlayer
local Shirt = ""-------Shirt Link Goes here
local Pants = ""-------Pants Link Goes Here
local PlayerShirt = game.Workspace:FindFirstChild(WhoCLick.Name).Shirt
local PlayerPants = game.Workspace:FindFirstChild(WhoCLick.Name).Pants
PlayerShirt.ShirtTemplate = Shirt
PlayerPants.PantsTemplate = Pants
end)
.FindFirstChild(function()) won't work.
Instead, do this (I used random templates for examples purposes):
script.Parent.MouseButton1Click:Connect(function(WhoClick)
WhoClick.Character.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=57282083"
WhoClick.Character.Clothing.Template = "http://www.roblox.com/asset/?id= 6714633844"
end)
I don't know where you got that code from but it cannot work.
You want to implement an event listener for the gui buttons MouseButton1Click event.
https://developer.roblox.com/en-us/api-reference/event/GuiButton/MouseButton1Click
Instead you try to call Instance:FindFirstChild with it.
Please refer to the Roblox manual.
Implement the listener as a function and call the buttons Connect function like in the following example. This will register your event listener to the click event.
function leftClick()
print("Left mouse click")
end
script.Parent.MouseButton1Click:Connect(leftClick)
Alternatively this will also work:
script.Parent.MouseButton1Click:Connect(function ()
print("Left mouse click")
end)

How to trigger a MouseButton1Click event in Roblox?

I have a button and I want to fire the event that happens when the player clicks on it by another script. I tried button.MouseButton1Click() but it didn't work. How can I achieve it?
If you want to reuse code, I would recommend looking into ModuleScripts. You can write your shared code functionality in the ModuleScript, and then use it in both places that you need it.
So in a ModuleScript in ReplicatedStorage, you might have something like :
local Foo = {}
function Foo.DoSomething()
print("Doing the thing!")
-- add your other behaviors here!
end
return Foo
Then, in your code with your button :
local Foo = require(game.ReplicatedStorage.Foo) -- put the path to your ModuleScript
local button = script.Parent
button.MouseButton1Click:Connect(function()
Foo.DoSomething()
end)
And you can do the same thing in another script too!
local Foo = require(game.ReplicatedStorage.Foo)
Foo.DoSomething()
This way you won't have to fake a mouse click, your code simply exists in a sharable location.
You need to connect the Click Event to a function:
button.MouseButton1Click:Connect(function()
--whatever code you want to happen after the button is clicked goes here
end)

Having trouble with some computercraft/lua code

Hi I want my lua code in Computercraft to allow the user to turn the redstone signal on/off by right clicking on a monitor on top, but I can't get it to work.
monitor = peripheral.wrap("top")
monitor.clear()
monitor.setTextColor(colors.red)
monitor.setCursorPos(1, 1)
monitor.setTextScale(1)
monitor.write("Hello")
function rubber()
monitor.setCursorPos(1, 2)
monitor.clearLine()
if rs.getOutput("right", true) then
monitor.write("Rubber farm is on")
elseif rs.getOutput("right", false) then
monitor.write("Rubber farm is off")
end
local event = { os.pullEvent() }
if event == "monitor_touch" then
if rs.getOutput("right") == true then
rs.setOutput("right", false)
else
rs.setOutput("right", true)
end
else
write("test")
end
rubber()
end
Right now all it displays is 'hello' and I don't know how to fix it, anyone know how? Also I'm a beginner at Lua so I've probably made some pretty simple mistakes. Thanks
local event = { os.pullEvent() }
if event == "monitor_touch" then
os.pullEvent returns a tuple. In your code, you're packing this tuple into a table. That's fine, but you then compare that table to a string. Tables can't be equal to strings - they're a table. Either don't pack the tuple into a table, and keep the first return value (the type):
local event = os.pullEvent()
if event == "monitor_touch" then
Or extract the first element when comparing
local event = { os.pullEvent() }
if event[1] == "monitor_touch" then
The problem is you wanted to have that function infinitly looping, but you have not called your function outside your function.... also you should look into using while loops
while true do
//stuff here
end
just add
rubber()
to the last line after your last end tag.
You have to call the function.
rubber()
You need to close your function
function rubber()
monitor.setCursorPos(1,1)
monitor.clearLine()
end
The end is it you need to make this little word
this is a simple fix, simply add rubber() after you finish the function rubber, cause while you have created the function rubber, you have not called for it to start yet.
The "monitor_touch" event is what you should be using. Also, make sure the monitor you are using is an advanced monitor (the one with the yellow border).
If you need help in understanding the event, check out this page: http://computercraft.info/wiki/Monitor_touch_(event)

World of Warcraft Lua - Changing frame:SetAttribute()

I'm working on an addon for World of Warcraft that completely overhauls the interface to adapt to my play style.
In this addon, I would like to have a large button that acts as a "main dps rotation" for my mage. I would like it to change what spell it casts based on what is optimal at any given time. It doesn't cast the spell automatically, it just presents the next best option for the user.
Here is my code so far:
print "Interface Overhaul : LOADED"
heatingUpIsActive = false
print(heatingUpIsActive)
local Button = CreateFrame("Button", "MyButton", UIParent,"SecureActionButtonTemplate")
Button:SetWidth(256)
Button:SetHeight(256)
Button:SetFrameStrata("HIGH")
Button:SetPoint("LEFT")
Button:SetText("Main Rotation")
Button:RegisterForClicks("AnyUp")
Button:SetAttribute("type", "spell")
Button:SetAttribute("spell", "Fireball")
Button:RegisterEvent("UNIT_AURA");
local function auraGained(self, event, ...)
if (UnitAura("player", "Heating Up")) then
if (heatingUpIsActive == false) then
heatingUpIsActive = true
print (heatingUpIsActive)
print ("Heating Up is active!")
Button:SetAttribute("spell", "Inferno Blast")
end
else
heatingUpIsActive = false
print("Heating Up is NOT active.")
print(heatingUpIsActive)
end
end
Button:SetScript("OnEvent", auraGained);
local tex = Button:CreateTexture("ARTWORK");
tex:SetPoint("LEFT")
tex:SetWidth(256)
tex:SetHeight(256)
tex:SetTexture("Interface\\AddOns\\InterfaceOverhaul\\Button2")
If heatingUpIsActive == true, I would like the button to cast ("spell", "Inferno Blast") instead of ("spell", "Fireball"), but it doesn't work if I place that into the correct part of the if statements.
Any thoughts?
As Mud said, you cannot rebind buttons in combat anymore. Blizzard made this change to prevent bots from being able to automate combat. Notably, in order to cast a spell you need to use one of the secure templates, and these secure templates only allow modification of the attributes that control what they do when you're not in combat. So you cannot have one button change spells mid-combat. Similarly, they also prevent you from modifying attributes like their position or visibility, so you cannot move buttons under the mouse either.
The best you can do is display a visual indicator of what spell should be cast, but rely on the user to actually press the correct button.

Resources