Having trouble implementing on-key functions

Hello there, I apologize in advance if I accidentally put this in the wrong category, I am very new to the forums.

I am having trouble putting in my code for an on-key function for playing animations

[Example: If you press Q on the keyboard the weapon plays a slash.]

My friend was able to help me with it but he made it into a local script instead of a tool.
I am wondering if anybody knows how to change it into a tool?

2 Likes

Local scripts can run in tools. If you parent the local script in the tool, it should work as intended. :slight_smile:

1 Like

It semi works.

When I place the local script in the tool, the local script works in the tool and off the tool, I just want it to work in the tool.

You could check if parent of the local script is a tool and only run the code if it is.

1 Like

I think it’s parented correctly.

HI. It’s not very clear to me what you’re asking, but you make it sound like you’re trying to transfer a script’s source code to a tool object? This can’t be done. The only executable script classes are the Script and LocalScript.

If, however, you need help with making the script work, We’d be glad to help. Please post the source here in this thread so we can see. Be sure to use code tags by surrounding your code with “</>” button!

Continuing what @mircostaff said, if you parent the localscript into the tool, you can have a Tool.Equipped connection change an Equipped variable to true, and Tool.Unequipped to change the variable to false.
An example is here:

Equipped = false

Tool.Equipped:connect(function()
     Equipped = true
end)

Tool.Unequipped:connect(function()
     Equipped = false
end

 game:GetService("UserInputService").InputBegan:connect(function(i, gpe)
     if gpe == false then --Checks if user is not in chat or any coregui menus
          if i.KeyCode == Enum.KeyCode.Q and Equipped then --Checks if the user pressed the Q key and tool is equipped
               --Stuff
          end
     end
end)
2 Likes

Here is some code I used in my custom physics system, you can feel free to use it if you like. I’ll provide the original code - as well as the simplified version for what you’re asking for.


Original:

game:GetService("UserInputService").InputBegan:connect(function(key,gameProcess)
	if not gameProcess then
		if key.KeyCode.Name == "A" then
			Inputs.a = 1
		end
		if key.KeyCode.Name == "S" then
			Inputs.s = 1
		end
		if key.KeyCode.Name == "D" then
			Inputs.d = 1
		end
		if key.KeyCode.Name == "W" then
			Inputs.w = 1
		end
	end
end)

Simplified:

game:GetService("UserInputService").InputBegan:Connect(function(key,gameProcess)
	if not gameProcess then
		if key.KeyCode.Name == "Q" then
			--[[Pathway To Animation]]:Play()
		end
	end
end)
2 Likes

Thank you very much for the source code, that was very generous of you.

I’ve been ripping my hair out trying to figure out how I can fix my sword without breaking it.

Alright here it goes, this is the code I made.

local plr = game.Players.LocalPlayer
local char = plr.Character

local model = script.Sword
for i,v in pairs(model:GetChildren()) do
if v:isA(“BasePart”) then
local weld = Instance.new(“Weld”)
weld.Parent = model.Main
weld.Part0 = model.Main
weld.Part1 = v
local CJ = CFrame.new(model.Main.Position)
local C0 = model.Main.CFrame:inverse()*CJ
local C1 = v.CFrame:inverse()*CJ
weld.C0 = C0
weld.C1 = C1
end
end

local weld = Instance.new(“Weld”)
weld.Parent = model.Main
weld.Part0 = model.Main
weld.Part1 = plr.Character[“Right Arm”]

model.Parent = char

game:GetService(“UserInputService”).InputBegan:connect(function(key,typ)
if not typ then
if key.KeyCode == Enum.KeyCode.Q then
local animation = Instance.new(“Animation”)
animation.AnimationId = “rbxassetid://1192762732”
local track = char.Humanoid:LoadAnimation(animation)
track:Play()
elseif key.KeyCode == Enum.KeyCode.E then
local animation = Instance.new(“Animation”)
animation.AnimationId = “rbxassetid://74894663”
local track = char.Humanoid:LoadAnimation(animation)
track:Play()
elseif key.KeyCode == Enum.KeyCode.F then
local animation = Instance.new(“Animation”)
animation.AnimationId = “rbxassetid://1199630444”
local track = char.Humanoid:LoadAnimation(animation)
track:Play()
end
end
end)

If the code is not readable I have taken screenshots of it as well
First half: Screenshot by Lightshot
Bottom half: Screenshot by Lightshot

No problem, have you fixed your issue yet or are you still having complications?

It appears I am still having complications, I don’t know where to put the code, do I replace the code I have or do I put it in a new script.

Hey again! I just noticed that your tool has no handle.This means the tool will have no held item, but the existence of the “Sword” group seems to suggest you’d like to have one. In your script, you seem to be making a weld to the player’s arm manually, and this is almost never what you want to do.

Tool Hierarchy

Currently, you have a model inside a LocalScript inside the tool object, and this is pretty much just wrong, to be honest. Don’t worry! We all make mistakes. What you’ll most likely want to do is something like this:


(Please note that the part you intend to use as the tool must have the Name property set as “Handle” [with the capital H!])

Also in the screenshot above, please take note of the GripPos value I’ve pointed out in the tool object. This property can be used to offset the tool if you find the character is not holding it correctly. (click the down arrow and try changing the Y value, if it’s too high or low!)
More info on the tool object, its properties, and the proper hierarchy of a tool can be found on the wiki: http://wiki.roblox.com/index.php/Tools

The LocalScript (You didn’t use the code tag! ;-; )

I’ve taken the liberty to import the script you sent into studio and have a little look-see. Honestly, I can tell the first bit where it welds all the parts together is copy-pasted. How, you ask? Well, you forgot to set the C0 and C1 properties when you welded to the arm. Practice makes perfect, so don’t sweat it too much!

After sorting through all this weld business, I noticed another big issue. you’re instantiating an animation object every time you press a key. Here’s why this is an issue: As the player presses the key repeatedly to make the animation play, a new object is added each time. This can (and will) lead to client slowdowns over time, and as Sonic says, “That’s no good!” I’d personally recommend creating the animations once, near the beginning of the script:

After doing this, and adjusting the input code to match, it works.
For more information and a nice example on animations: http://wiki.roblox.com/index.php?title=Animations#Example

But there’s another problem that I simply can’t solve for you!

Sad but true, two of your animations don’t seem to load correctly. I’ve kept the ids as they were in the original script, and while the animation for the “E” key works, the other two do not. You may need to go find or upload some new animations!

CONCLUSION
Scripting is hard, I get it. It can take a lot of time and effort, but it really is rewarding. I think it’s wonderful that you’re making a tool, and give you my full support!

Here’s the finished script, but please do take the time to read the post, if you haven’t!

local plr = game.Players.LocalPlayer
local char = plr.Character

local animQ = Instance.new("Animation")
animQ.AnimationId = "rbxassetid://1192762732"
local animE = Instance.new("Animation")
animE.AnimationId = "rbxassetid://74894663"
local animF = Instance.new("Animation")
animF.AnimationId = "rbxassetid://1199630444"

game:GetService("UserInputService").InputBegan:connect(function(key,typ)
	if not typ then
		if key.KeyCode == Enum.KeyCode.Q then
			local track = char.Humanoid:LoadAnimation(animQ)
			track:Play()
		elseif key.KeyCode == Enum.KeyCode.E then
			local track = char.Humanoid:LoadAnimation(animE)
			track:Play()
		elseif key.KeyCode == Enum.KeyCode.F then
			local track = char.Humanoid:LoadAnimation(animF)
			track:Play()
		end
	end
end)
2 Likes

Since this thread will probably be used for future reference, I decided to code a more flexible version of what @wiggle1000 posted.

Not sure if there is much difference, but I remove the old tracks.

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character ~= nil
local char = plr.Character
local lastTrack

Animations = {
	["Q"] = 1192762732;
	["E"] = 74894663;
	["F"] = 1199630444;
}

for keyBtn,id in pairs(Animations) do
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://"..tostring(id)
	Animations[keyBtn] = anim
end

game:GetService("UserInputService").InputBegan:connect(function(input,typ)
	if not typ then
		for keyBtn, anim in pairs(Animations) do
			if input.KeyCode.Name == keyBtn then
				if lastTrack ~= nil then
					lastTrack:Stop()
					lastTrack = nil
				end
				lastTrack = char.Humanoid:LoadAnimation(anim)
				lastTrack:Play()
			end
		end
	end
end)
2 Likes

Hey. Did you ever figure this out, @CheetahSp33d? If so, please mark a message as the answer.

He/she marked your response as the solution a while ago.

1 Like

I’m a dude, but I respect you for not assuming anything.

On the side note, how would I add sound?

Thank you to all who helped me.

1 Like