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

Keydown function wont work inside normal script?

i need it to be in a normal script for this to work but they keydown function only seems to work in a local script

does anyone know why it only works in a local script?

--//Keystroke class
local debounce = true

script.Parent.Equipped:Connect(function()
	local tool = script.Parent
	local char = tool.Parent
	local player = game.Players:GetPlayerFromCharacter(char)
	local mouse = player:GetMouse()
	local v = script:WaitForChild('Value')
	mouse.KeyDown:Connect(function(key)
		print (key)
		-- stuff
	end)
end)

1 answer

You cannot get the player's mouse in a Server Script. You can use a RemoteEvent for this to work.

Local Script:

--//Keystroke class
local debounce = true

script.Parent.Equipped:Connect(function()
    local tool = script.Parent
    local char = tool.Parent
    local player = game.Players:GetPlayerFromCharacter(char)
    local mouse = player:GetMouse()
    local RemoteEvent = game.ReplicatedStorage.RemoteEvent
    local v = script:WaitForChild('Value')
    mouse.KeyDown:Connect(function(key)
        RemoteEvent:FireServer(key)
    end)
end)

Server Script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, Key)

	--Code
	print(Key)
end)

Let me know if it doesn't work. I'll try to help you.

4 comments

0

works perfectly, thank you

0

No problem, happy to help.

0

Don't forget to accept my answer if it worked.

0

Also, mouse.KeyDown is deprecated. You should now always use UserInputService.InputBegan.

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