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

best way to make a equip and unequip function for custom tools?

Asked by 5 years ago

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)

2 answers

Log in to vote
0
Answered by
oilkas 364 Moderation Voter
5 years ago

You can do this by using "ChildAdded" and "ChildRemoved" functions. You CAN use modules for this.

Server Sided Script:

01game.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)
14end)

With Modules:

Server Sided Script:

01local ToolModule = require(ModulePathHere)
02 
03game.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)
16end)

Module:

01local ToolModule = {}
02 
03ToolModule.Equip = function()
04    -- Do equipped code
05end
06ToolModule.UnEquip = function()
07    -- Do unequipped code
08end
09 
10return ToolModule
0
The thing is i have different equip functions for each item for example a apple i want the player to click then have it destroy and for example a chair i want the player to click the ground then place it there so im guessing i need tons of "ToolModule.Equip = function()" fistter1 88 — 5y
0
no no, you can do this: in the function, add a parameter of the item, then you can do the code like: if item == "apple" then do equipped code oilkas 364 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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:

01game.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)
17end)

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.

Answer this question