Pass values to functions from Onclicks roblox

To pass values to functions from Onclicks in Lua for Roblox, you can follow these steps:

  1. Define the function that will receive the values as parameters:
local function onClickFunction(value1, value2)
    -- Your code here
end
  1. Set up the OnClick event to call the function and pass the values:
local button = script.Parent -- Replace with the actual button reference
button.MouseButton1Click:Connect(function()
    local val1 = "some value" -- Replace with the actual value
    local val2 = 123 -- Replace with the actual value
    onClickFunction(val1, val2)
end)
  1. Inside the onClickFunction, you can now use the passed values as needed:
local function onClickFunction(value1, value2)
    print("Value 1: " .. value1)
    print("Value 2: " .. value2)
    -- Your code using the values here
end

This approach allows you to define a function that accepts parameters, set up an event to trigger the function with specific values when the button is clicked, and then process the passed values inside the function as required.