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

Adding a keydown to this function?

Asked by
iFlusters 355 Moderation Voter
8 years ago

This is not a request, but I was wondering where would I add a KeyDown function in this function, I am really not sure and would need some help.

Basically when the key, example "e" is pressed it would FireServer()

function onTouched(hit)
    local Humanoid = hit.Parent:FindFirstChild("Humanoid")
    if not Humanoid then
        return;
    end

    if not script.Parent:WaitForChild("ButtonEvent") then
        script.Parent:Destroy()
    return;
    end

    script.Parent.ButtonEvent:FireServer()
end

script.Parent.Touched:connect(debounce(onTouched))

1 answer

Log in to vote
1
Answered by
rexbit 707 Moderation Voter
8 years ago

Since KeyDown is a event of mouse you could access player from hit by using the :GetPlayerFromCharacter.

function onTouched(hit)
    local Humanoid = hit.Parent:FindFirstChild("Humanoid")
    if not Humanoid then
        return;
    end

    if not script.Parent:WaitForChild("ButtonEvent") then
        script.Parent:Destroy()
    return;
    end
    local pl = game.Players:GetPlayerFromCharacter(hit.Parent)
    local mouse = pl:GetMouse()
    mouse.KeyDown:connect(function(key)
        if key == "e" then
            script.Parent.ButtonEvent:FireServer()
        end
    end)
end

script.Parent.Touched:connect(debounce(onTouched))
Ad

Answer this question