Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Equip tools via a script?

Is it even possible to make a player equip a tool via a script (e.g. when he/she presses a certain key)? Any help is appreciated, as I do not have any idea how to make it...

2 answers

✓ Accepted answer

Anything is possible with a script!

Breaking down the problem

All you need to do is ask yourself what to do first, then focus on that. The last thing you want to do is get an idea of something to accomplish without any steps. So, here's what we need to ask:

1: How are tools equipped in the first place?

Tools are equipped by placing a Tool object inside a player's character. You can see this transition by paying close attention to your character as you equip a tool in your inventory in studio.

2: How would I trigger something to happen when a key is pressed?

UserInputService is a neat UI that allows us to compare and connect information when a user inputs certain data. In this case, a KeyCode.

(UserInputService: http://wiki.roblox.com/index.php?title=API:Class/UserInputService)

We can make a simple hello world program when the 'e' key is pressed using this demonstration:

local UserInput = game:GetService("UserInputService")

UserInput.InputBegan:connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		print("hello world") -- prints 'hello world' when 'e' is pressed
	end
end)

Combining solved problems

Now all we have to do is combine all our solved problems, and create a final result. In this case, all we have to do is make it so a tool appears inside a player's character when a key is pressed. Here's the result:

local UserInput = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()

-- Create a tool
local Tool = Instance.new("Tool")
Instance.new("Part",Tool).Name = "Handle"

UserInput.InputBegan:connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		Tool.Parent = Character -- Give the player the tool
	end
end)

Hope this helped.

1 comment

0

Thank you!

Well if you were to search up KeyDown you would get the part of he/she presses down a key. And this was in the forum the other day but the Humanoid has a function called EquipTool. Instead of KeyDown I use UserInputService as KeyDown is deprecated. An example would be um:

local UserInputService = game:GetService'UserInputService'
local p = game.Players.LocalPlayer
repeat wait() until p.Character
local c = p.Character

UserInputService.InputBegan:connect(function(inputObject, gameProcessedEvent) -- key goes down
	if not gameProcessedEvent then -- so it doesn't activate when you chat
		if inputObject.KeyCode == Enum.KeyCode.F then -- F down
			c.Humanoid:EquipTool(game.ReplicatedStorage.Tool) -- tells humanoid to equip tool
		end
	end
end)

Related links may include: http://wiki.roblox.com/index.php?title=API:Class/Humanoid/EquipTool http://wiki.roblox.com/index.php?title=API:Class/Mouse/KeyDown http://wiki.roblox.com/?title=API:Class/UserInputService http://wiki.roblox.com/index.php?title=API:Class/InputObject http://wiki.roblox.com/index.php?title=API:Class/InputObject/KeyCode

This discussion is preserved as it was. New answers, comments, and votes are closed.