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:
01 | game.Players.PlayerAdded:Connect( function (Player) |
02 | Player.CharacterAdded:Connect( function (Character) |
03 | Character.ChildAdded:Connect( function (AddedChild) |
04 | if AddedChild:IsA( 'Tool' ) then -- Check if child is a tool |
05 | -- Do equipped code |
06 | end |
07 | end ) |
08 | Character.ChildRemoved:Connect( function (RemovedChild) |
09 | if RemovedChild:IsA( 'Tool' ) then -- Check if child is a tool |
10 | -- Do unequipped code |
11 | end |
12 | end ) |
13 | end ) |
14 | end ) |
With Modules:
Server Sided Script:
01 | local ToolModule = require(ModulePathHere) |
02 |
03 | game.Players.PlayerAdded:Connect( function (Player) |
04 | Player.CharacterAdded:Connect( function (Character) |
05 | Character.ChildAdded:Connect( function (AddedChild) |
06 | if AddedChild:IsA( 'Tool' ) then -- Check if child is a tool |
07 | ToolModule.Equip() |
08 | end |
09 | end ) |
10 | Character.ChildRemoved:Connect( function (RemovedChild) |
11 | if RemovedChild:IsA( 'Tool' ) then -- Check if child is a tool |
12 | ToolModule.UnEquip() |
13 | end |
14 | end ) |
15 | end ) |
16 | end ) |
Module:
01 | local ToolModule = { } |
02 |
03 | ToolModule.Equip = function () |
04 | -- Do equipped code |
05 | end |
06 | ToolModule.UnEquip = function () |
07 | -- Do unequipped code |
08 | end |
09 |
10 | 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:
01 | game.Players.PlayerAdded:Connect( function (player) |
02 | player.CharacterAdded:Connect( function (character) |
03 | function unequipTool(tool) |
04 | tool.Parent = player.Backpack |
05 | end |
06 |
07 | function equipTool(tool) |
08 | tool.Parent = character |
09 | end |
10 |
11 | -- Unequip |
12 | unequipTool(game.ServerStorage.Tool:Clone()) |
13 |
14 | -- Equip |
15 | equipTool(game.ServerStorage.Tool:Clone()) |
16 | end ) |
17 | 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.