So in Roblox, as you may know, pressing backspace while equipping an item, will cause the item to drop, if you don’t want that to happen you can toggle the CanBeDropped option off, but the problem with that is that it will unequip the item. Games like Arsenal seem to handle this by resetting the character whenever the player when pressing backspace, but the game I am working on is very much orientated around KOs and WOs so I don’t want the player accidentally pressing backspace and giving themselves a WO, here is a solution I came up with, but I am wondering if anyone else knows a more efficient solution
game:GetService("UserInputService").InputBegan:Connect(function(keyCode)
if keyCode.UserInputType == Enum.UserInputType.Keyboard then
if keyCode.KeyCode == Enum.KeyCode.Backspace then
local tool = game.ReplicatedStorage.Hammer:Clone()
tool.Parent = game.Workspace[game.Players.LocalPlayer.Name]
game.Players.LocalPlayer.Character.Humanoid:EquipTool(tool)
end
end
end)
The script is meant to make sure the item doesn’t unequip when the player presses backspace, the script works for me, I’m just wondering if there is a more efficient solution
Not really unless you wanted to check for child removing and see if its a tool. Then check the name to make sure it matches up and puts that tool back into the character/Backpack.
A better solution would be to use the ChildRemoved event of the player’s Character to detect when something is unparented from them, such as a tool being unequipped. You could then determine if that child is a tool, and then equip it to them if it is.
Would probably look like this
player.Character.ChildRemoved:Connect(function(child)
if child:IsA("Tool") then
child.parent = player.Character
end
end)
Going off of @itsLevande code, I refined the code to work without the warning about unexpectedly setting the tool’s parent. Anyways, here’s the final code:
-- Place in ServerScriptService
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character.ChildRemoved:Connect(function(child)
if child:IsA("Tool") and child.Parent == workspace then
-- Wait to avoid a potential warning about unexpectedly setting the parent
task.wait()
child.Parent = player.Character
end
end)
end)
end)
The proper way to do this is to use defer so that it’s reparented to the character in the same frame or at the earliest invocation point of the next frame. Never use wait to solve problems.
Ideally though, if you’re looking to prevent the character from backspacing tools, I’d highly recommend making a custom tool system. The whole reason why developers make custom tool systems is to have more control over the behaviour of tools, including removing the ability to backspace-unequip them.
If Arsenal resets players upon backspace, that’s just because it’s a convenient enough key. I’m pretty sure Arsenal doesn’t use tools, but if it did that’d be surprising. I’d wager that they use a custom tool system and backspace therefore happens to be a free key to conveniently reset the character.