How can I change it so that instead of it requiring you to hit a key, it requires you to a click the imagebutton?

Hello there, instead of wanting my script to fire an event when a key is clicked, I want it to fire of a mouseclick. How would I make this work?

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local ragdollbutton = game.StarterGui["Toggle ragdoll"]
local button = ragdollbutton:WaitForChild("ImageButton")
local ClickDetector = Instance.new("ClickDetector")

function ToggleVariables(mode)
	if mode == "R" then
		Events_Toggle:FireServer("R", not Variables_RagdollClient.Value)
	elseif mode == "L" then
		Events_Toggle:FireServer("L")
	end
end

Events_GuiToClient.Event:Connect(ToggleVariables)

game:GetService("ContextActionService"):BindAction("RagdollToggle", function(_,input)
	if input == Enum.UserInputState.Begin then
		ToggleVariables("R")
	end
end, false, Enum.KeyCode.R)

game:GetService("ContextActionService"):BindAction("ResetHotkey", function(_,input)
	if input == Enum.UserInputState.Begin then
		ToggleVariables("L")
	end
end, false, Enum.KeyCode.L)

(post withdrawn by author, will be automatically deleted in 1 hour unless flagged)

I don’t understand, please explain! I’m new to lua sorry.

If it is a GUI image, then I would cover it up with a 0.99 transparent button. When the button is clicked, it will fire.

If it is a image on a part, same thing.

(don’t know if this works, just came up with this solution, or if image buttons have events where they can detect clicks use that)

edit:
turns out, yes, there are click events for image buttons.
https://developer.roblox.com/en-us/api-reference/class/ImageButton
just make your functions or whatever you want to fire use MouseButton1Click and MouseButton2Click

Example:

local ImageButton = the image button

local function Fire()
       --bla bla bla
end

ImageButton.MouseButton1Click:Connect(Fire)
ImageButton.MouseButton2Click:Connect(Fire)

Is this correct because it doesn’t do anything at all when the guibutton is clicked (yes its a imagebutton)

read the last lines

function ToggleVariables(mode)
	if mode == "R" then
		Events_Toggle:FireServer("R", not Variables_RagdollClient.Value)
	elseif mode == "L" then
		Events_Toggle:FireServer("L")
	end
end

Events_GuiToClient.Event:Connect(ToggleVariables)

game:GetService("ContextActionService"):BindAction("RagdollToggle", function(_,input)
	if input == Enum.UserInputState.Begin then
		ToggleVariables("R")
	end
end, false, Enum.KeyCode.R)

game:GetService("ContextActionService"):BindAction("ResetHotkey", function(_,input)
	if input == Enum.UserInputState.Begin then
		ToggleVariables("L")
	end
end, false, Enum.KeyCode.L)

button.MouseButton1Click:Connect(function()
	ToggleVariables("R")
end)```

1:
Is there any errors showing up in output?
2:
What is “button”
Is it a variable? I don’t see any variables.

no there aren’t any errors

this is actually another script and im trying to understand it so the variables are what keys meant to be clicked in order to fire an event (guessing)

I meant this part, what is “button” ?
I do not see any variable defining button.

1 Like

How many times do I met people using StarterGui

use:

local ragdollbutton = Player:WaitForChild("ScreenGui name here")

(and yes use MouseButton1Click/Down like other people said)

oh button is the image button, there’s a local defining it inside the script somewhere

MouseButton1Click should work.

Double check that the server is receiving the event with a print function

okay ill change that then 12345

I looked at the script, at every line. I have not found this local. Could you provide me it?

edit: nevermind, I was looking at his second post that didn’t have the variables.

wait what is the difference between “game” and “player”?

local ragdollbutton = Player.StarterGui["Toggle ragdoll"]
local button = ragdollbutton:WaitForChild("ImageButton")

Yep, sorry, was looking at your second post which provided the script but without any variables.

hold on it says startergui is not a valid member of player

game is a DataModel

Player is an instance that’s basically, yknow, player

I’m sorry I wrote it wrong

do this instead

local ragdollbutton = Player:WaitForChild("PlayerGui"):WaitForChild("Toggle ragdoll")
1 Like