Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What would be the best way to simulate a linked source?

Asked by 6 years ago

I've been looking for a way to make the number of scripts I have to change in a game of mine smaller (i.e. I'd only have to change one script), problem I haven't been able to find a way to do that locally, I've tried something to the tune of private modules (i.e. require(game:GetService("ReplicatedStorage").modulescript) but I'm not sure how to get the module script to execute relative to the script that's calling it (or really how t execute at all, I'm thinking it should work somthing like an anti-exploit loader). I've looked into having a script clone the source of another script into itself, but I'm still not sure how to go about that. Any ideas?

Thanks in advance.

1 answer

Log in to vote
1
Answered by 6 years ago

Solutions:

  1. game.ReplicatedStorage.SomeScript:Clone().Parent = script.Parent; script:Destroy()
  2. ModuleScripts (you could put your code in the ModuleScript, wrap it in a function, and call it; see below)
  3. Redesign your scripts to use tables/lists instead of copy/pasting your script for each item (you can also use ModuleScripts as collections of functions and/or places to hold data to be shared amongst multiple Scripts)

ex. Let's say you have a script that just keeps changing the transparency of its parent and want to change it so you don't have to copy/paste the script:

--Before
local p = script.Parent
while true do
    for i = 0, 1, 0.1 do
        p.Transparency = i
        wait()
    end
    for i = 0.9, 0.1, -0.1 do
        p.Transparency = i
        wait()
    end
end

--Option #2, ModuleScript:
return function(script)
    --above code here
end

--Option #2, calling script:
require(game.ReplicatedStorage.TransparencyScript)(script)

--Option #3, central script:
local ch = workspace.Parts:GetChildren()
--or if the parts aren't all in the same place:
local ch = {workspace.Part1, workspace.SomeModel.Part2, --[[etc]]}
while true do
    for i = 0, 1, 0.1 do
        for j = 1, #ch do
            ch[j].Transparency = i
        end
        wait()
    end
    for i = 0.9, 0.1, -0.1 do
        for j = 1, #ch do
            ch[j].Transparency = i
        end
        wait()
    end
end

I recommend option #3 as you can script which parts to use, rather than having to copy/paste the requiring script in option #2; it also is easier to coordinate changes to multiple objects when it's all handled in the same script. ex, if in the above scenario you wanted to make it so that the 2nd object was a bit delayed in its transparency changes compared to the 1st, there are ways to do that, and you'd only have to change this one script.

0
regarding option #3, imagine doing that with lots of coins... chabad360 34 — 6y
0
tho honestly i went with option #1 chabad360 34 — 6y
0
I'm not sure what you mean by "lots of coins" (or what you mean by that line) - done properly, option #3 is easier to maintain/modify than option #1 - after all, there's only the one script, rather than several. Option #1 is still reasonable, and preferable if you aren't good with tables yet. chess123mate 5873 — 6y
Ad

Answer this question