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

Equip tools via a script?

Asked by 9 years ago

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

Log in to vote
3
Answered by 9 years ago

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:

1local UserInput = game:GetService("UserInputService")
2 
3UserInput.InputBegan:connect(function(Input)
4    if Input.KeyCode == Enum.KeyCode.E then
5        print("hello world") -- prints 'hello world' when 'e' is pressed
6    end
7end)

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:

01local UserInput = game:GetService("UserInputService")
02local Player = game:GetService("Players").LocalPlayer
03local Character = Player.Character or Player.CharacterAdded:wait()
04 
05-- Create a tool
06local Tool = Instance.new("Tool")
07Instance.new("Part",Tool).Name = "Handle"
08 
09UserInput.InputBegan:connect(function(Input)
10    if Input.KeyCode == Enum.KeyCode.E then
11        Tool.Parent = Character -- Give the player the tool
12    end
13end)

Hope this helped.

0
Thank you! TheArmoredReaper 173 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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:

01local UserInputService = game:GetService'UserInputService'
02local p = game.Players.LocalPlayer
03repeat wait() until p.Character
04local c = p.Character
05 
06UserInputService.InputBegan:connect(function(inputObject, gameProcessedEvent) -- key goes down
07    if not gameProcessedEvent then -- so it doesn't activate when you chat
08        if inputObject.KeyCode == Enum.KeyCode.F then -- F down
09            c.Humanoid:EquipTool(game.ReplicatedStorage.Tool) -- tells humanoid to equip tool
10        end
11    end
12end)

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

Answer this question