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

Click key to equip a weapon, can anyone help?

Asked by
Akozta 40
8 years ago

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

Log in to vote
1
Answered by
woodengop 1134 Moderation Voter
8 years ago

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)
Ad
Log in to vote
0
Answered by
funyun 958 Moderation Voter
8 years ago

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
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 DragonODeath 50 — 8y

Answer this question