Do Localscripts fire immediely in PlayerScripts?

Im working on a tool that when you unequip it, it adds a LocalScript to the player’s PlayerScripts.

--Main LocalScript--
local plr = game.Players.LocalPlayer
local tool = script.Parent
local playerScript = tool:FindFirstChild("LocalScriptExample")
tool.Equipped:Connect(function()
--some code here
end)
tool.Unequipped:Connect(function()
local LSCClone = playerScript:Clone()
LSCClone.Parent = plr:WaitForChild("PlayerScripts")
end)
--LocalScriptExample--
print("Yeet!")

i have 2 problems with this:

  1. LocalScriptExample always fires when the tool is equipped
  2. LocalScriptExample doesnt fire when it gets cloned into PlayerScripts

im sure fixing problem 1 would be easy, but so far i havent been able to get any LocalScripts inside PlayerScripts (or StarterPlayerScripts) to work at all!
Im not sure if what im doing is wrong, or if PlayerScripts just hates me putting LocalScripts into it.

You could have the script set as disabled, and then you could move it and set disabled to false, which should restart the script and run the main thread of it.

local LSCClone = playerScript:Clone()
LSCClone.Parent = plr:WaitForChild("PlayerScripts")
LSCClone.Disabled = false

this fixed it running immediately when the tool is equipped.

i also have another problem, whenever i try to clone the LocalScript into PlayerScripts, it never clones it into PlayerScripts but doesnt give an error saying it failed to clone into PlayerScripts.

I believe roblox doesn’t allow you to add anything to it at runtime, as they are all executed when the player loads. If you are looking for that functionality, add the code to the player backpack. You could also add it to the character model, just keep in mind that it will destroy when the player dies. Good luck!

changed the script to clone it in the backpack and it worked! thanks for the help!

1 Like