Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Click key to equip a weapon, can anyone help?

So I have a hard time with coding, like I know the words, I just don't know the structure. If someone could help me structure this better I would appreciate it.

script.Parent.Selected:connect(function(k)
        m.KeyDown:connect(function(k)
                print(k)
						if k=="q" then
local tool=script.parent
tool.Equipped:connect(function(KeyDown)
m.KeyDown:connect(function(k)     
end)
end)
end)
end)
script.Parent.Selected:connect(function() 
end)

2 answers

✓ Accepted answer

If you don't quite understand the Structure of the script, i'll show you.

local tool=script.Parent

A variable is a Title that holds a value, the Variable shown above is currently holding script.Parent as it's value. Next we need an Function, some functions can be already defined, and some can be custom.

-- predefined function
object.Touched:connect(function(parameter)
	return parameter
end)

-- undefined function
local ABC=function(a,b,c) -- a,b,c as the function's parameter
	return print(a,b,c)
end

--(parameters are variables kept in a function)

But if you want a defined function then we use events, What is an event? An event is an Object that connects to a user-defined function(listeners), when the certain event ever happens, the Event fires. In this case an KeyDown event.

local tool -- define tool
local player=game.Players.LocalPlayer
local character=player.Character
local humanoid=character:WaitForChild("Humanoid")

player:GetMouse().KeyDown:connect(function( key )
	if key==" q " then
		humanoid:EquipTool(tool)
	end
end)

If by structure you mean tabbing and where to put things, I cleaned this up for you:

tool = script.Parent --Ideally, you want to declare some variables in the beginning of the script.

script.Parent.Selected:connect(function(mouse) --The "Selected" event passes the mouse as an argument to its connected function.
	mouse.KeyDown:connect(function(key)
		if key == "q" then --You could also write these 3 commands out on 1 line, since they're so short.
			print(key)
		end
	end) --Closing the KeyDown function
end) --Closing the Selected function

1 comment

0

@OP seems like a fm. Merh. HopperBins have taken a turn for the worse. There are better ways to do things with the character

This discussion is preserved as it was. New answers, comments, and votes are closed.