How do I activate/deactivate a script while a game is running

One of my scripts should not run before I send a command from a server script for it to run

Model.Script.Enabled = true

but this line does not enable the script nor does it return an error in the output so I don’t know how to fix this.

Help would be appreciated :+1:

3 Likes

Why don’t you try using remote events or remote functions? If you’re wanting cross communication between server - server boundary then you should use bindable events or bindable functions.

2 Likes

thats a script right? not a local script

If you’re wanting to enable/disable an active script, I assume that it involves code that is constantly active. To do this, there’s no need to have code in separate scripts unless it’s for organization. You can simply use the task library to achieve full control on the running of your code.

If you pass a function to task.spawn() it will run the passed function in a new thread, and return the newly active thread (known as a coroutine).

By storing access to that thread in a variable, you can cancel it whenever you want with task.cancel(). By passing an active thread to this function, it immediately stops further execution of the thread on the task scheduler.

Here’s what it might look like set up:

local function ThreeLetters()
	print('A')
	task.wait(1.5)
	print('B')
	task.wait(1)
	print('C')
end

local New_Thread = task.spawn(ThreeLetters) --Runs ThreeLetters in a new thread. Does not yield.
task.wait(2) --Wait 2.5 seconds into ThreeLetters.
task.cancel(New_Thread) --Cancels the execution of the running thread for ThreeLetters returned by task.spawn

--A and B should print, but the function should be canceled before getting to C.

hmm thinking about this

characters minimum

The thing is, I am cloning a model from ServerStorage to have its script run on Workspace, so it cannot be run on the same script

Can you be a little more specific with your exact goal you are trying to achieve? You should never disable/enable scripts during run time.

Based on what I gather, you can have a single script in ServerScriptService to manage all these models that you are cloning to the workspace, rather than having a copy of the script for each model you clone.

Ok so for my goal, I’m trying to prevent a script from running (initially put into ServerStorage) until a server script clones it and puts it into workspace. The thing is that when a client script clones and puts the script into workspace, it returns an Infinite yield warning as the server script provides one of the variables required for that script to run

It would be hard to put the script as a single script in ServerScriptService as my existing code is very messy and limiting, making it quite difficult to put stuff without making major adjustments to the code, which takes time

nvm ive got the problem all wrong