Starting a Function from another script

Hi all,

I have a script which contains a function to regen a model - script is inside the model in the workspace. I need this function to run when a button in a PlayerGUI is pressed, but not sure how to go about this. I have tried just referencing the button from the main script, but that didn’t seem to work, and due to the setup I am unable to put this regen code inside the GUI button. Does anybody have any ideas of how to go about getting this function to run when the GUI button is clicked?

1 Like

Make the button fire a remote event on the server which activates the function assuming the script is on the server.

How would I set this up, I assume a local script inside the button but what lines of code would need to be in there, and what would need to be added to the main script to run the function when button pressed?

I assume you know enough to make this work for you properly.

The LocalScript:

local event = game.ReplicatedStorage:WaitForChild("OnClick") 
script.Parent.MouseButton1Click:Connect(function()
    event:FireServer()
end)

The Script:

local event = game.ReplicatedStorage:WaitForChild("OnClick")
local function cloneModel()
     --Do whatever
end

event.OnServerEvent:Connect(cloneModel)
2 Likes

Works as intended… I had tried this before but obviously not properly. Thank you for your help! :slight_smile:

1 Like