What happens to local scripts when they are removed from a character/player?

Hey,

I’m making a turret system, and I want guns to unequip when you enter it. The only issue is that I am also using a custom inventory system that requires additional handling. My tactic right now is to reserve the tool under replicated storage and then put it back under the character when they exit the turret.

This is fine and all, but I need to be able to detect this removal on the tool and then fire an “unequip” function, otherwise, the animations still play.

So, my question is, when I move it to replicated storage, will the script still work? Can I detect when this happens on the tool client script and then trigger the unequip function?

Any help is appreciated. Thanks.

1 Like

When you move a local script into ReplicatedStorage, it’s going to stop running immediately so it’s unlikely that the tool client script will be able to detect itself moving into a container where it can’t run. You might need to have a separate script that checks when children are removed from the character and have the logic in that script.

1 Like

I think you could make it so when u enter the turret u unequip a tool if the player is holding one and stop the animation there, then u move them into RepStorage.
Also I personally would make a localscript/modulescript parenteded somewhere in RepStorage with an initialize function that runs everytime a tool is added in the player’s backpack inside a CharacterAdded. This way you dont have scripts in the different tools and having issues of the local script bugging out.

Im making a game with this type of system and i have made this (it works fine for me)

local function init()
	local char = player.Character or player.CharacterAdded:Wait()
	
	for _, tool in player.Backpack:GetChildren() do
		if tool.Name == "Tool" then
			Tool.initialize(tool, char)
		elseif tool.Name == "AnotherTool" then
			AnotherTool.initialize(tool, char)
		end
	end
end

function module.onStart()
	init() -- run when player joins
	
	task.delay(1, function()
          --add delay so u dont risk init() running twice on join
		player.CharacterAppearanceLoaded:Connect(function()
			init() -- everytime player respawns u reinitialize the tools because the previous ones were destroyed on death
		end)
	end)
end

This doesnt account for new items being equipped but i think you could just make a ChildAdded for the backpack and do it there. Ofc you would make different checks to make sure that tool wasnt already in the backpack and its actually a new tool the player has found
And i make a char variable inside init() because the first time it runs maybe the character didnt load in time or stuff like that

You would have a main module script doing this and then use a function of the item’s module (i have a modular framework). Or you make this run in each tool’s local/module scripts but I’d rather have it organized in one script requiring the tools’ modules.

1 Like

Thank you for the in-depth response.

I stopped using my inventory system so I switched over to humanoid:UnequipTools(), however beforehand I figured out that you could add a delay before removing the tool with a bindable event.

The original reply did technically answer my question, but thank you for a possible solution.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.