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?
01 | wait() |
02 | local toolname = script.Parent.Name |
03 | local replicatedstorage = game:GetService( "ReplicatedStorage" ) |
04 | local fruit = replicatedstorage.Skills:WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value) |
05 | local tool = fruit:WaitForChild(script.Parent.Name) |
06 | local plr = game.Players.LocalPlayer |
07 | local plrc = plr.Character |
08 | Mouse = plr:GetMouse() |
09 |
10 |
11 |
12 | function 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() |
I agree with what the above person said; UserInputService
is the better, easier, and more developed method.
1 | User 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
1 | local userInputService = game:GetService( "UserInputService" ) |
2 |
3 | userInputService.InputBegan:connect( function (key) |
4 | if key.KeyCode = = Enum.KeyCode.E then |
5 | print ( "The letter E was pushed" ) |
6 | end |
7 | end ) |
Here is this implemented into your script:
01 | local plr = game.Players.LocalPlayer |
02 | repeat wait() until plr.Character |
03 | local plrc = plr.Character |
04 | local toolName = script.Parent.Name |
05 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
06 | local fruit = replicatedstorage:WaitForChild( "Skills" ):WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value) |
07 | local tool = fruit:WaitForChild(script.Parent.Name) |
08 | local uis = game:GetService( "UserInputService" ) |
09 |
10 | uis.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 |
17 | end ) |
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!
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:
01 | wait() |
02 | local toolname = script.Parent.Name |
03 | local replicatedstorage = game:GetService( "ReplicatedStorage" ) |
04 | local fruit = replicatedstorage.Skills:WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value) |
05 | local tool = fruit:WaitForChild(script.Parent.Name) |
06 | local plr = game.Players.LocalPlayer |
07 | local plrc = plr.Character |
08 | local UIS = game:GetService( "UserInputService" ) |
09 |
10 |
11 |
12 | function PressE(input, gpe) |
13 | if input.KeyCode = = Enum.KeyCode.E then |
14 | if script.Parent.Reloading.Value = = "No" then |
15 | local toolCloning = tool:Clone() |
ContextActionService method:
01 | wait() |
02 | local toolname = script.Parent.Name |
03 | local replicatedstorage = game:GetService( "ReplicatedStorage" ) |
04 | local fruit = replicatedstorage.Skills:WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value) |
05 | local tool = fruit:WaitForChild(script.Parent.Name) |
06 | local plr = game.Players.LocalPlayer |
07 | local plrc = plr.Character |
08 |
09 |
10 |
11 | function 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 |
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.