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

How i add in this script, the function to equip the tool too?

Asked by 6 years ago

i created a script that clone the tool from replicatedStorage, and put in the backpack when press E, but i want it to equip the tool too when press E, but isnt working, there is a other way to do this?

01wait()
02local toolname = script.Parent.Name
03local replicatedstorage = game:GetService("ReplicatedStorage")
04local fruit = replicatedstorage.Skills:WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value)
05local tool = fruit:WaitForChild(script.Parent.Name)
06local plr = game.Players.LocalPlayer
07local plrc = plr.Character
08Mouse = plr:GetMouse()
09 
10 
11 
12function PressE(key) --Edit PressQ to what ever key you want. Example: PressF (capitals matter)
13    if (key == "e") then -- Edit "q" to the same key you chose in the line above! Example: "f" (make sure it isn't in caps!)
14        if script.Parent.Reloading.Value == "No" then
15        local toolCloning = tool:Clone()
View all 23 lines...

2 answers

Log in to vote
0
Answered by
Async_io 908 Moderation Voter
6 years ago
Edited 6 years ago

I agree with what the above person said; UserInputService is the better, easier, and more developed method.

User Input Service

1User input service is fairly easy to use and is actually better than the other service you're currently using. It's also a lot more versatile as you can use `InputBegan`; `InputChanged`; `InputEnded`, `JumpRequest()`; `GamepadConnected`; etc etc etc. Basically, if you're ever planning on making your game multi-platform, you should consider using `UserInputService`. By what you're trying to do, I would suggest `InputBegan`.

Here's an example script

1local userInputService = game:GetService("UserInputService")
2 
3userInputService.InputBegan:connect(function(key)
4    if key.KeyCode == Enum.KeyCode.E then
5        print("The letter E was pushed")
6    end
7end)

Here is this implemented into your script:

01local plr = game.Players.LocalPlayer
02repeat wait() until plr.Character
03local plrc = plr.Character
04local toolName = script.Parent.Name
05local replicatedStorage = game:GetService("ReplicatedStorage")
06local fruit = replicatedstorage:WaitForChild("Skills"):WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value)
07local tool = fruit:WaitForChild(script.Parent.Name)
08local uis = game:GetService("UserInputService")
09 
10uis.InputBegan:connect(function(key)
11    if key.KeyCode == Enum.KeyCode.E and script.Parent.Reloading.Value == "No" then
12        local toolCloning = tool:Clone()
13        toolCloning.Parent = plr.Backpack
14        wait(0.25)
15        plrc.Humanoid:EquipTool(toolCloning)
16    end
17end)

Another thing: I noticed that you used script.Parent.Parent.Parent.Parent.Backpack.toolname which isn't the proper method. If you're looking for an item via it's name and it's not in string format, then use script.Parent.Parent.Parent.Parent.Backpack[toolname] as this will look for the tool with toolname instead of literally toolname.

If this helped, then please accept my answer!

1
the script doesn't work anymore :( darkzerobits 92 — 6y
1
oh, wait a minute darkzerobits 92 — 6y
1
it worked, thanks darkzerobits 92 — 6y
Ad
Log in to vote
2
Answered by
LeadRDRK 437 Moderation Voter
6 years ago
Edited 6 years ago

You are using the Mouse.KeyDown event, which is deprecated and most likely will not work. UserInputService and ContextActionService is more preferred over.

So there are two method to do this.

UserInputService method:

01wait()
02local toolname = script.Parent.Name
03local replicatedstorage = game:GetService("ReplicatedStorage")
04local fruit = replicatedstorage.Skills:WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value)
05local tool = fruit:WaitForChild(script.Parent.Name)
06local plr = game.Players.LocalPlayer
07local plrc = plr.Character
08local UIS = game:GetService("UserInputService")
09 
10 
11 
12function PressE(input, gpe)
13    if input.KeyCode == Enum.KeyCode.E then
14        if script.Parent.Reloading.Value == "No" then
15        local toolCloning = tool:Clone()
View all 22 lines...

ContextActionService method:

01wait()
02local toolname = script.Parent.Name
03local replicatedstorage = game:GetService("ReplicatedStorage")
04local fruit = replicatedstorage.Skills:WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value)
05local tool = fruit:WaitForChild(script.Parent.Name)
06local plr = game.Players.LocalPlayer
07local plrc = plr.Character
08 
09 
10 
11function PressE(actionName, userInputState, inputObject)
12    if userInputState == Enum.UserInputState.Begin then
13        if script.Parent.Reloading.Value == "No" then
14        local toolCloning = tool:Clone()
15        toolCloning.Parent = plr.Backpack
View all 21 lines...

Either way can be used and they will work as expected.

If it still doesn’t work: check if the Reloading value is set to "No" and your tool is named "toolname" (because the script refers it)

Hope this helps.

0
still doesn't work :( , he give the tool, but he don't equip it :( darkzerobits 92 — 6y
0
I don't think he literally meant to equip toolname. I think he was looking for the tool that has the same name as the script (which is stored in the variable toolname) Async_io 908 — 6y
0
ye ye darkzerobits 92 — 6y

Answer this question