Enable a script through a GUI button

Does anyone know how to activate a script in workspace, via pressing a GUI button.

I have this, but it does not seem to be working:

local on = false

script.Parent.MouseButton1Click:Connect(function()
    if on then
		workspace.MovementScript.Disabled = true
        on = false
    else
		workspace.MovementScript.Disabled = false
        on = true
    end
end)```
1 Like

ignore this, sorry i didn’t know disabled was a thing for scripts, i only knew enabled, my bad

(i just found out)

1 Like

redone:

local script = workspace.MovementScript
local button = script.Parent

button.MouseButton1Click:Connect(function()
    script.Enabled = not script.Enabled
end)

i think this would work just fine

1 Like

You can simplify this script to be:


script.Parent.MouseButton1Click:Connect(function()
	
	workspace.MovementScript.Disabled = not workspace.MovementScript.Disabled

end)

It will only activate a LocalScript.

I don’t believe a LocalScript will run in workspace.

2 Likes

a localscript cannot run in the workspace, it can run in other things like tools, startergui, repfirst, startercharscripts and starterplrscripts

2 Likes

all true other than replicatedstorage

2 Likes

yeah, just reread that and realized i put repstore :sob: (ty for the correction)

2 Likes

It’s not running a local script, have I made a mistake with the code where it thinks its running a local script?

1 Like

they are stating that it will only work if the script is local

OHHHH, hold on I will try this

It still won’t work, could it have anything to do with the contents of the script?

local serverStorage = game:GetService("ServerStorage")
local workspace = game:GetService("Workspace")

local modelNameToImport = "Owner1" 
local function importModel()
	local modelTemplate = serverStorage:FindFirstChild(modelNameToImport)
	if modelTemplate and modelTemplate:IsA("Model") then
		local modelClone = modelTemplate:Clone()
		modelClone.Parent = workspace

		-- Ungroup the model
		for _, child in ipairs(modelClone:GetChildren()) do
			child.Parent = workspace
		end
		modelClone:Destroy() -- Destroy the original model after ungrouping
	else
		warn("Model template '" .. modelNameToImport .. "' not found in ServerStorage or is not a Model.")
	end
end

importModel()

wait(3)

local npc = game.Workspace.Owner1
local checkpoint1 = game.Workspace.Ocheckpoint1
local checkpoint2 = game.Workspace.Ocheckpoint2
local checkpoint3 = game.Workspace.Ocheckpoint3
local checkpoint4 = game.Workspace.Ocheckpoint4
local checkpoint5 = game.Workspace.Ocheckpoint5
local checkpoint6 = game.Workspace.Ocheckpoint6
local checkpoint7 = game.Workspace.Ocheckpoint7
local checkpoint8 = game.Workspace.Ocheckpoint8


local idleAnimation = game.Workspace.Owner1.Animation1
local walkAnimation = game.Workspace.Owner1.WalkAnim
local Annoyed = game.Workspace.Owner1.FireplacePose
local Annoyed2 = game.Workspace.Owner1.FireplacePose2

local idleAnimationTrack
local walkAnimationTrack
local FireplacePoseTrack
local FireplacePose2Track


wait(0.1)

idleAnimationTrack = npc.Humanoid:LoadAnimation(idleAnimation)
walkAnimationTrack = npc.Humanoid:LoadAnimation(walkAnimation)
FireplacePoseTrack = npc.Humanoid:LoadAnimation(Annoyed)
FireplacePose2Track = npc.Humanoid:LoadAnimation(Annoyed2)

idleAnimationTrack:Play()

wait(5)
npc.Humanoid:MoveTo(checkpoint1.Position)
idleAnimationTrack:Stop()
walkAnimationTrack:Play()
npc.Humanoid.MoveToFinished:Wait()
npc.Humanoid:MoveTo(checkpoint2.Position)
npc.Humanoid.MoveToFinished:Wait()
npc.Humanoid:MoveTo(checkpoint3.Position)
npc.Humanoid.MoveToFinished:Wait()
npc.Humanoid:MoveTo(checkpoint4.Position)
npc.Humanoid.MoveToFinished:Wait()
walkAnimationTrack:Stop()
wait(9)
walkAnimationTrack:Play()
npc.Humanoid:MoveTo(checkpoint5.Position)
npc.Humanoid.MoveToFinished:Wait()
walkAnimationTrack:Stop()
FireplacePoseTrack:Play()
wait(4)
FireplacePoseTrack:Stop()
FireplacePose2Track:Play()
wait(3)
walkAnimationTrack:Play()
npc.Humanoid:MoveTo(checkpoint5.Position)
npc.Humanoid.MoveToFinished:Wait()
npc.Humanoid:MoveTo(checkpoint6.Position)
npc.Humanoid.MoveToFinished:Wait()
npc.Humanoid:MoveTo(checkpoint7.Position)
npc.Humanoid.MoveToFinished:Wait()
npc.Humanoid:MoveTo(checkpoint8.Position)
npc.Humanoid.MoveToFinished:Wait()




checkpoint1:Destroy()
checkpoint2:Destroy()
checkpoint3:Destroy()
checkpoint4:Destroy()
checkpoint5:Destroy()
checkpoint6:Destroy()
checkpoint7:Destroy()
checkpoint8:Destroy()
game.Workspace.Owner1:Destroy()

maybe add a print to the target script that you enabling to make sure it actually enables

I did, I removed it to copy and paste here

did it print at all?

because if it did then theres a problem with your script

Didn’t print, maybe I should try a remote event?

Edit: script works well when its enabled from the get go (as in I have selected enabled before loading the game)

no, i have 2 different questions for you

where is the script?

what type of script?

in workspace, and it’s probably a serverscript as it’s trying to access ServerStorage

1 Like

MovingScript (the one I am trying to enable) is in workspace, and I have made it a local script after reading a reply here.

The second script, is located in a text button GUI, which I am hoping will be able to enable the MovingScript

With that being said, server scripts wont run if you enable them from the client.

local scripts dont run in workspace