Weapon Take When On Part, Give Back When Not On Part

Hey there! I have been working on this script that I couldn’t get to work correctly. Basically, what it is supposed to do is when a player walk into a part (and stays on the part,) they get their tool(s) taken away. But when they step off of the part, they get their tool(s) back. Kind of like a sword fighting arena. Anyway, I looked for a while and couldn’t find anything that would help. I only found a couple scripts that were a little outdated and some that weren’t exactly what I’m looking for. Thanks!

Ok when the player touches the block that removes parts do,

-- Script inside a server script
game.Players.PlayerAdded:Connect(function(player)
      local PlayerFolder = Instance.new("Folder", game.ReplicatedStorage) -- We'll need somewhere to hold the players tools
      PlayerFolder.Name = player.Name.." Folder" -- Names the folder based on the player so we can access it later
end)

game.Players.PlayerRemoving:Connect(function(player)
      local PlayerFolder = game.ReplicatedStorage:FindFirstChild(player.Name.." Folder")
      if PlayerFolder then
             PlayerFolder:Destroy() -- Removes that player's folder when they leave
      end
end)

-- Script inside the remover part
script.Parent.Touched:Connect(function(hit) -- Gets brick that touched block
       local Player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Checks if a player was the one who touched the block
       if Player then -- The function will equal nil if it couldn't find a player related to that object's parent.
           local Tools = Player.Backpack:GetChildren() -- Unequipped tools are parented here
           local EquippedTools = Player.Character:GetChildren() -- Player could have equipped tools that aren't parented to backpack
           for i, v in pairs(Tools) do   -- Runs code for each object parented to the backpack which in this case will be tools
                 local PlayerFolder = game.ReplicatedStorage:FindFirstChild(Player.Name.." Folder")
                 if PlayerFolder then
                        v.Parent = PlayerFolder
                 end
           end
           for i, v in pairs(EquippedTools) do   -- Runs code for each object parented to the player's character and checks if it is a tool before removing it and putting it in the player's storage folder
                 local PlayerFolder = game.ReplicatedStorage:FindFirstChild(Player.Name.." Folder")
                 if PlayerFolder then
                        if v:IsA("Tool") then
                               v.Parent = PlayerFolder
                        end
                 end
           end
       end
end)

-- Script parented to block that gives items back

script.Parent.Touched:Connect(function(hit) -- Gets brick that touched block
       local Player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Checks if a player was the one who touched the block
       if Player then 
           local PlayerFolder = game.ReplicatedStorage:FindFirstChild(Player.Name.." Folder")
           if PlayerFolder then
                local Tools = PlayerFolder:GetChildren()
                for i, v in pairs(Tools) do
                     v.Parent = Player.Backpack
                end
           end
       end
end)

This should work, I don’t have access to studio currently so I can’t test it.

3 Likes