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

Why does this Local Script run fine but does not work with KeyDown function?

Asked by 9 years ago

This local script works fine with everything but then when the keydown function comes it is like the script skipped over it and continued on with everything else every time.

local tool = script.Parent

local player = game.Players.LocalPlayer
local character = player.Parent

local firerate = 1
local range = 200


local handle = tool:WaitForChild("Handle")
local event = tool:WaitForChild("RemoteEvent")
local firesound = handle:WaitForChild("FireSound")


local check = true


function checkintangible(hit)
    if hit and hit ~= nil then
        if hit:IsDescendantOf(tool.Parent) or hit.Name == "Handle" or string.lower(string.sub(hit.Name, 1, 6)) == "effect" then
            return true
        end
    end
    return false
end

function castray(startpos, vec, length, ignore, delayifhit)
    local hit, endpos2 = game.Workspace:FindPartOnRay(Ray.new(startpos, vec * length), ignore)
    if hit ~= nil then
        if checkintangible(hit) then
            hit, endpos2 = castray(endpos2 + (vec * .01), vec, length - ((startpos - endpos2).magnitude), ignore, delayifhit)
        end
    end
    return hit, endpos2
end

tool.Equipped:connect(function(mouse)
    equipped = true
    if mouse then
        mouse.Icon = "rbxasset://textures\\GunCursor.png"
        mouse.Button1Down:connect(function()
            local humanoid = tool.Parent:FindFirstChild("Humanoid")
            local head = tool.Parent:FindFirstChild("Head")
            local torso = tool.Parent:FindFirstChild("Torso")
            if check and humanoid and humanoid.Health > 0 and head and torso then
                check = false
                mouse.Icon = "rbxasset://textures\\GunWaitCursor.png"

                firesound:Play()

                local vec = (mouse.Hit.p - head.Position).unit
                local hit ,endpos = castray(head.Position, vec, range, tool.Parent)

                event:FireServer("Fire", endpos, hit)

                wait(firerate)
                if mouse then
                    mouse.Icon = "rbxasset://textures\\GunCursor.png"
                end
                check = true
            end
        end)
        mouse.KeyDown:connect(function(key)
            key = key:lower()
            if key == "m" then
                for _, part in pairs(character:GetChildren())do
                    if part.ClassName == "Torso" then
                        part.Transparency = 1
                    elseif part.ClassName == "Head" then
                        part.Transparency = 1
                    elseif part.ClassName == "Right Arm" then
                        part.Transparency = 1
                    elseif part.ClassName == "Left Arm" then
                        part.Transparency = 1
                    elseif part.ClassName == "Right Leg" then
                        part.Transparency = 1
                    elseif part.ClassName == "Left Leg" then
                        part.Transparency = 1
                    end
                end
            end
        end)
    end
end)

tool.Unequipped:connect(function()
    equipped = false
end)

Answer this question