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

Attempt to index global 'key' (a nil value)?

Asked by 8 years ago

I'm trying to make a weapon that plays an animation when you press the R key.

15:45:56.829 - Players.Player.Backpack.Tool.Laser:47: attempt to index global 'key' (a nil value)

How would I fix this?

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
    down = true;
    while down do

        local ray = Ray.new(tool.Red.CFrame.p, (mouse.Hit.p - tool.Red.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Really red")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (tool.Handle.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(tool.Red.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

        game:GetService("Debris"):AddItem(beam, 0.1)

        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid then
                humanoid:TakeDamage(0)
            end
        end
        wait();
end;
end);

    mouse.Button1Up:connect(function()
down = false;
end);
end)

key.KeyDown:connect(function(h)
if h=="R" then print("lol this is a test") end 
end)

0
It's because you used key.KeyDown instead of mouse.KeyDown on line 47. OneTruePain 191 — 8y
0
Still gives me the same error... TehPootisman_DxD 44 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

KeyDown is deprecated, use UserInputService.

local uis=game:GetService("UserInputService")

uis.InputBegan:connect(function(key)
    if key.KeyCode==Enum.KeyCode.R then --really no need for lower
        print'User pressed R!'
    end
end)
0
Got an error    18:41:30.448 - Players.Player.Backpack.Spectrum Engine.Red:50: bad argument #1 to 'lower' (string expected, got EnumItem) TehPootisman_DxD 44 — 8y
0
LEL, edited, sorry. SimplyRekt 413 — 8y
Ad
Log in to vote
0
Answered by
iiscrx 0
3 years ago

Ik this was 4 years ago but ima still answer, make a variable for Mouse

game.Players.PlayedAdded:Connect(function(Player)
local mouse = Player:GetMouse()
mouse.KeyDown:connect(function(h)
if h == "r" then
print("lol this is a test")
end
end)

this should work

Answer this question