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 5 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?

wait()
local toolname = script.Parent.Name
local replicatedstorage = game:GetService("ReplicatedStorage")
local fruit = replicatedstorage.Skills:WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value)
local tool = fruit:WaitForChild(script.Parent.Name)
local plr = game.Players.LocalPlayer
local plrc = plr.Character
Mouse = plr:GetMouse()



function PressE(key) --Edit PressQ to what ever key you want. Example: PressF (capitals matter)
    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!)
        if script.Parent.Reloading.Value == "No" then
        local toolCloning = tool:Clone()
        toolCloning.Parent = plr.Backpack
        wait(0.25)
        plrc.Humanoid:EquipTool(script.Parent.Parent.Parent.Parent.Backpack.toolname)

    end
  end
end
Mouse.KeyDown:connect(PressE) --Edit (PressQ) to the same key you chose in line 8 and 9. Example: PressF

2 answers

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

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

User Input Service

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

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:connect(function(key)
    if key.KeyCode == Enum.KeyCode.E then
        print("The letter E was pushed")
    end
end)

Here is this implemented into your script:

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local plrc = plr.Character
local toolName = script.Parent.Name
local replicatedStorage = game:GetService("ReplicatedStorage")
local fruit = replicatedstorage:WaitForChild("Skills"):WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value)
local tool = fruit:WaitForChild(script.Parent.Name)
local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(key)
    if key.KeyCode == Enum.KeyCode.E and script.Parent.Reloading.Value == "No" then
        local toolCloning = tool:Clone()
        toolCloning.Parent = plr.Backpack
        wait(0.25)
        plrc.Humanoid:EquipTool(toolCloning) 
    end
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!

1
the script doesn't work anymore :( darkzerobits 92 — 5y
1
oh, wait a minute darkzerobits 92 — 5y
1
it worked, thanks darkzerobits 92 — 5y
Ad
Log in to vote
2
Answered by
LeadRDRK 437 Moderation Voter
5 years ago
Edited 5 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:

wait()
local toolname = script.Parent.Name
local replicatedstorage = game:GetService("ReplicatedStorage")
local fruit = replicatedstorage.Skills:WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value)
local tool = fruit:WaitForChild(script.Parent.Name)
local plr = game.Players.LocalPlayer
local plrc = plr.Character
local UIS = game:GetService("UserInputService")



function PressE(input, gpe) 
    if input.KeyCode == Enum.KeyCode.E then
        if script.Parent.Reloading.Value == "No" then
        local toolCloning = tool:Clone()
        toolCloning.Parent = plr.Backpack
        wait(0.25)
        plrc.Humanoid:EquipTool(script.Parent.Parent.Parent.Parent.Backpack.toolname)
    end
  end
end
UIS.InputBegan:Connect(PressE)

ContextActionService method:

wait()
local toolname = script.Parent.Name
local replicatedstorage = game:GetService("ReplicatedStorage")
local fruit = replicatedstorage.Skills:WaitForChild(script.Parent.Parent.Parent.Parent.Stats.AkumaNoMi.Value)
local tool = fruit:WaitForChild(script.Parent.Name)
local plr = game.Players.LocalPlayer
local plrc = plr.Character



function PressE(actionName, userInputState, inputObject) 
    if userInputState == Enum.UserInputState.Begin then
        if script.Parent.Reloading.Value == "No" then
        local toolCloning = tool:Clone()
        toolCloning.Parent = plr.Backpack
        wait(0.25)
        plrc.Humanoid:EquipTool(script.Parent.Parent.Parent.Parent.Backpack.toolname)
    end
  end
end
game.ContextActionService:BindAction("keyPress", PressE, false, Enum.KeyCode.E)

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 — 5y
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 — 5y
0
ye ye darkzerobits 92 — 5y

Answer this question