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

How to use a keyDown event in another one?[Solved]

Asked by 9 years ago

I want to make a part appear at my right arm when I press the key B and to remove it when I press B again.I tried some ways to remove the part but it didn't work.It didn't say anything in the output.Basically all I want to know is how to make an event after another one started.Here's the code for making the part:

Player = script.Parent.Parent
c = Player.Character
mouse = Player:GetMouse()
activate = false
function onMouseButton1Down(Key)
    activate = true
    key = Key:lower()
    if key == "b" then
    c["Right Arm"].Transparency = 1
    x = Instance.new("Part")
    x.TopSurface = "Smooth"
    x.BottomSurface = "Smooth"
    x.Size = c["Right Arm"].Size
    x.BrickColor = BrickColor.new("Black")
    x.Transparency = 0
    x.Reflectance = 0.1
    x.Parent = c
    w1 = Instance.new("Weld", c)
    w1.Part0 = w1.Parent["Right Arm"]
    w1.Part1 = x
    w1.C1 = CFrame.fromEulerAnglesXYZ(0, 0, 0) *CFrame.new(0, 0, 0)
    end 
end
mouse.KeyDown:connect(onMouseButton1Down)

3 answers

Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

Well, one way you could approach this issue is by setting a Boolean to (true/false) every time the "b" key is pressed, and based on what the value is, code whatever you want to happen.

Tips:

Since you can only get the player's mouse via LocalScripts, you can just get the player using game.Players.LocalPlayer

The player's character might take a little longer to completely load, therefore, an easier way to wait for the character is by using Player.CharacterAdded:Wait()

local player = game:GetService('Players').LocalPlayer
local character = player.Character or player.CharacterAdded:wait()

local mouse = player:GetMouse()
local bool = false

mouse.KeyDown:connect(function(key)
    if key:lower()== 'b' then
        bool = not bool

        if bool and wait() then
            print(true)
        else
            print(false)
        end
    end
end)

I re-named a few variables for and spaced it all out a bit just to keep it all clean and easier to understand.

Ad
Log in to vote
1
Answered by
yumtaste 476 Moderation Voter
9 years ago

You don't need to do that. Really, all you have to do is use variables to keep track of whether or not the part is there. Just a suggestion, it should be easy to implement, if you need me to show you how to do that just comment.

Player = script.Parent.Parent
c = Player.Character
mouse = Player:GetMouse()
activate = false
local partIsActivated = false
function onMouseButton1Down(Key)
    activate = true
    key = Key:lower()
    if key == "b" then
    if not partIsActivated then
    c["Right Arm"].Transparency = 1
    x = Instance.new("Part")
    x.TopSurface = "Smooth"
    x.BottomSurface = "Smooth"
    x.Size = c["Right Arm"].Size
    x.BrickColor = BrickColor.new("Black")
    x.Transparency = 0
    x.Reflectance = 0.1
    x.Parent = c
    w1 = Instance.new("Weld", c)
    w1.Part0 = w1.Parent["Right Arm"]
    w1.Part1 = x
    w1.C1 = CFrame.fromEulerAnglesXYZ(0, 0, 0) *CFrame.new(0, 0, 0)\
    elseif partIsActivated then
    --put the code here to remove the part, or you can make it so the part is just visible or invisible. up to you
    end
    end 
end
mouse.KeyDown:connect(onMouseButton1Down)
0
Yeah, I'd like to see how. brokenrares 48 — 9y
0
There. Check that out, and reply if you have further questions. yumtaste 476 — 9y
0
Thanks! brokenrares 48 — 9y
0
No problem. yumtaste 476 — 9y
Log in to vote
0
Answered by 9 years ago

I've fixed the script that should make the toggle work. It should allow you to toggle the arm and the real roblox arm.

Player = script.Parent.Parent
c = Player.Character
mouse = Player:GetMouse()
activate = false

function onMouseButton1Down(Key)
    local key = Key:lower()
    if key == "b" then
    if activate == false then
    activate = true
    c["Right Arm"].Transparency = 1
    local x = Instance.new("Part")
    x.TopSurface = "Smooth"
    x.BottomSurface = "Smooth"
    x.Size = c["Right Arm"].Size
    x.BrickColor = BrickColor.new("Black")
    x.Transparency = 0
    x.Reflectance = 0.1
    x.Parent = c
    x.Name = "FakeArm"
   local  w1 = Instance.new("Weld", c)
    w1.Part0 = w1.Parent["Right Arm"]
    w1.Part1 = x
    w1.C1 = CFrame.fromEulerAnglesXYZ(0, 0, 0) *CFrame.new(0, 0, 0)
    else
    activate = false
    c.FakeArm:remove()
     c["Right Arm"].Transparency = 0
    end
end
end
mouse.KeyDown:connect(onMouseButton1Down)

Answer this question