Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Adding a keydown to this function?

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

✓ Accepted answer

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))

This discussion is preserved as it was. New answers, comments, and votes are closed.