Need help running a script from a button

Hello everyone!

I have a button on inside a frame on StarterGui (game.StarterGui.Frame.Button) and when I press that button I want it to run a script which is located in Lightning (game.Lightning.Script), how should I make this script or how should it be?

Thank you in advance!

2 Likes

You should use a ModuleScript to do that:

2 Likes

You could use this:

local Button = script.Parent

Button.MouseButton1Down:Connect(function()
    local Lighting = game:GetService("Lighting")
    local Script = Ligthing:FindFirstChild("Script")
    Script.Disabled = false --Make sure to disable the script in lighting!
end)

I hope that helps

I’m not even certain that scripts run inside Lighting, I’d be surprised if they do.
It’d be best if you placed the Script in ServerScriptService

Your best approach here is to integrate a RemoteEvent creating a communication between a LocalScript inside your StarterGui and your Script.

The connected function to the MouseButton1Click should :FireServer() this RemoteEvent, then on the recieving end your Script can connect upon OnServerEvent of this RemoteEvent and run whatever’s needed.

For example:

--// LocalScript
local Button = script.Parent.Button
local Remote = game:GetService("ReplicatedStorage").RemoteEvent

Button.MouseButton1Click:Connect(function()
    Remote:FireServer()
end)

--// Script
local Remote = game:GetService("ReplicatedStorage").RemoteEvent

Remote.OnServerEvent:Connect(function()
    --// Code
end)
2 Likes

Can we see what you have scripted so far so we can troubleshoot the script?

1 Like