How do I insert/call a script into a running game?

I am trying to have a script that calls for another script once the game starts running.

To further clarify, I am trying to make it so I can easily update my script amongst multiple models but only need to change one script/asset, this includes models in different games. Any help is appreciated!

You could you use a ModuleScript, to store code, then run it in the first script, or you could disable and enable the script when you want, although I don’t know if the second one actually works. You could try testing it!

Yes, but could I have this as just an asset the game requires somehow? This needs to work over a mass amount of Games. It is for a model I am selling in which if something breaks/ needs to be updated I could easily update the script and it fixes it in all the games.

I see what you’re saying, but unfortunately I don’t know how to do that, sir.

afaik, you can just update the script in the uploaded asset, and it would automatically update, but that’s probably not true, because security issues.

I Think I can do:

require(AssetID)

The only issue is I have never made a module script before. So this is gonna be a new one for me. I will update everyone if this actually ends up working. If anyone knows any way to do it with just a normal server script let me know.

For your use case, just use the module as a normal script. Importing will start it.

Though, you should consider using InsertService

Just tested the InsertService Idea. This will not work because if someone buys my product it will just show the error “403 Forbidden”. Unless there is some way to fix that.

Usually you can use a coroutine, maybe run some code in the background, or spawn, have code fire as soon as something happens?

Use module scripts and use require in each of the your models.

Module scripts are pretty simple.It works like a script but instead of running it it stores them (like how modular components work in real life.) (Note that you should store functions and variables ONLY in these scripts otherwise you wont be able to access it)

Example :
Module

local module = {} --this is default dont mess with this

return module-- this is default dont mess with this (change the name if you wish to but just make sure its the same above as well)

How to make a function in module

local module = {} --this is default dont mess with this

function module.DoStuff(argument)
--Do stuff
end

--OR

function module:DoStuff(argument)
--Do stuff
end

return module-- this is default dont mess with this (change the name if you wish to but just make sure its the same above as well)

Using a module in a script

local module = require(YOUR_ASSET_ID)
module.DoStuff('ArgumentsIfAny')
module:DoStuff('ArgumentsIfAny')
1 Like

Not sure if this is what you’re looking for but you can try it out, it’s one of my common functions I use when I need to update a script or model in real time in game.

function CommonFunctions:ReplaceAsset(newAssetID, location, replace)
	local model = game:GetService("InsertService"):LoadAsset(newAssetID);
	for each, thing in pairs(model:GetChildren()) do
		if location:FindFirstChild(thing.Name) and replace then
			location[thing.Name]:Destroy();
		end
		thing.Parent = location;
	end
	model:Destroy();
end

--for example
CommonFunctions:ReplaceAsset(10441045920, game:GetService("ServerScriptService"), true)

This I believe is probably what I will end up doing. I will update you if I get it working. Probably gonna take a few hours.

Okay after running a few tests this will not work out the way I planned it to. This means I would have to have it for free available on the marketplace in order for a different account to require it. This would defeat the purpose because the way I am wanting to use this script is so I can sell my product and verify the person actually owns it.

Oh. Didnt know if that was what you wished for.
I suppose you should follow @GamerOkami 's Advice.If you run the code in the command bar it should do.but i dont think that would save you from the pain of updating it.

Edit : I am not sure but can you try making the script offsale and then try. Im not sure but i think it could work out. I saw it in Alexnewtron’s scripts. And im pretty sure they still work.

you can enable and disable scripts:
so in properties there is an enabled bool value set it to false
and then set it to true from another script when u want it to start running

So you want to be able to distribute your script to those who purchased it and only allow those to use it? You should put a whitelist in your script which checks if they own your product or not, and also obfuscate your script so when you do distribute it no one can edit your script to bypass the whitelist.

--you would obfuscate this whole script
local MarketplaceService = game:GetService("MarketplaceService")
local gameOwnerWhitelisted = MarketplaceService:UserOwnsGamePassAsync(game.CreatorId, YOUR_PRODUCT_ID)
if gameOwnerWhitelisted then
    --insert your script here
else
    warn("The creator of this game is not on the whitelist for this script")
    script:Destroy()
end

If you want to insert a line into a running game, you could use command bar.
image