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

How to Move a Script on Play Mode?

Asked by 8 years ago

Hello, I want to make a function that moves the script from Workspace to ServerScriptStorage when I press "Play". How do I do that?

3 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Parent


Whenever you want to change the directory an object is located in with a script, you can use the Parent property to set it somewhere else. For example:

local part = Instance.new("Part") -- Created a part with no parent

part.Parent = workspace -- Parenting the part to workspace

part.Parent = game:GetService("ServerStorage") -- Parenting the part to ServerStorage

The same method applies for changing any object's parent (including scripts). However, I don't see a practical use for moving the location of a script with another script. There have been many features added in ROBLOX that were specifically designed for organization and better communication between machines. Just put the server script in the ServerScriptService when in edit mode, and that shouldn't have to move it anywhere else.

Recommendations

Personally, I recommend you look into ReplicatedFirst, Remotes, and the difference between local scripts and server scripts. This will help you better understand a lot about how your game actually works, and opens up a lot more possibilities when developing!

Ad
Log in to vote
0
Answered by 8 years ago

Before you start the Test Solo, drag the script and drop it under 'ServerScriptStorage' (ServerScriptService), and press 'Test Solo', and you will see that the script is now found under your desired location!

Log in to vote
0
Answered by 8 years ago

Great question!

This is about something called parenting. If you look in the explorer window, you will find an overview of the objects within your game. Your reference to ServerScriptStorage indicates you are acquainted with it, so I won't be explaining it, or other objects again.

If you were to click and drag your script from the Workspace to the ServerScriptStorage it would change its Parent. Very much as child/parent relationships work in the real world, the parent is responsible for the child, and therefore holds it. Your script is the child of Workspace before you press play. You wish to write a script that changes your script, the child's parent to ServerScriptStorage.

To change the parent of an object within a script, you use object.Parent = newparent. In your script, you would replace object with script, and newparentwith game.ServerScriptStorage. When your game is started, this will change your script's parent from Workspace to ServerScriptStorage. If you looked in the explorer window again, your script would appear to be a child of ServerScriptStorage.

This is an overview of the parenting system. You can always set an object's parent by doing object.parent = newparent, regardless of the object (assuming it isn't something like Workspace or ServerScriptStorage, which are provided by ROBLOX to contain your objects).

NOW HEAR THIS Notice the game before ServerScriptStorage in script.Parent = game.ServerScriptStorage. This occurs because ServerScriptStorage is a child of game. Game encloses everything, including the objects shown when you start up a new file, however it is not displayed in the explorer. You MUST use it before any of the "startup objects" or your script will throw an error.

I hope this was helpful, and good luck.

Answer this question