Ok so im trying to figure out how i can make a specific code run when a specific object is put into the character
My current way of doing this is cloning a script in replicated storage that has the objects name then putting it inside the character, though my problem is i want it to execute unequip code when the object is destroyed (which is impossible since all connections are disconnected) So is there a better way to do this? Im thinking it has something to do with modules probably
think of equip code and unequip code stored in a script i want another script to require that unequip code when a object is destroyed (the tool)
You can do this by using "ChildAdded" and "ChildRemoved" functions. You CAN use modules for this.
Server Sided Script:
game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) Character.ChildAdded:Connect(function(AddedChild) if AddedChild:IsA('Tool') then -- Check if child is a tool -- Do equipped code end end) Character.ChildRemoved:Connect(function(RemovedChild) if RemovedChild:IsA('Tool') then -- Check if child is a tool -- Do unequipped code end end) end) end)
With Modules:
Server Sided Script:
local ToolModule = require(ModulePathHere) game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) Character.ChildAdded:Connect(function(AddedChild) if AddedChild:IsA('Tool') then -- Check if child is a tool ToolModule.Equip() end end) Character.ChildRemoved:Connect(function(RemovedChild) if RemovedChild:IsA('Tool') then -- Check if child is a tool ToolModule.UnEquip() end end) end) end)
Module:
local ToolModule = {} ToolModule.Equip = function() -- Do equipped code end ToolModule.UnEquip = function() -- Do unequipped code end return ToolModule
You need to set the parent of the tool to either a player's backpack or character. Tools are automatically equipped when they are placed in a player's character and unequipped when placed in a player's backpack. Try this. Make sure it's a regular/server script:
game.Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) function unequipTool(tool) tool.Parent = player.Backpack end function equipTool(tool) tool.Parent = character end -- Unequip unequipTool(game.ServerStorage.Tool:Clone()) -- Equip equipTool(game.ServerStorage.Tool:Clone()) end) end)
First, a player gets added. Then we wait for the player's character to get added. Then we have two functions that set the tool's parent to either the player's character or its backpack. The parameter is used for a Tool instance.