How do I make it so when a gui button is clicked, it fires the same function?

Hi there, I have a script where if you hit a key, such as “R” it fires a function however I want it to also allow clicks from a gui too and fire the same function, if that makes sense?


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)

Use .MouseButton1Click function on the button you want.

so for example

button.MouseButton1Click:Connect(function()
      ToggleVariables(“R”)
end)

how would i put this inside the script and where? sorry im new to lua and is still trying to understand everything

Hello!

function YourFunction(Name)
      print("Hello "..tostring(Name)) -- Your Function
end

script.Parent.MouseButton1Click:Connect(function())
      YourFunction("vxs")
end)

This script should be local script and parented in your gui dont forget that your gui need to be textbutton or imagebutton to work
and everytime you want to call the function you just need to write the name like this

function YourFunction(Name)
      print("Hello "..tostring(Name)) -- Your Function
end

YourFunction("Yes")

Edit: Also using local script will affect only Client and i see you want achieve something which will be for server so i recommend you taking a look at this
Remote Event This will allow you making changes for everyone using local script And with using it you just need to fire it when GUI being clicked

Have a good day!

1 Like