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 4 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
4 years ago

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
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 — 4y
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 — 4y
Ad
Log in to vote
0
Answered by 4 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:

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.

Answer this question