Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Keydown function wont work inside normal script?

Asked by 3 months ago
Edited 3 months ago

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

Log in to vote
2
Answered by 3 months ago
Edited 3 months ago

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.

0
works perfectly, thank you Metraria 15 — 3mo
0
No problem, happy to help. xInfinityBear 951 — 3mo
0
Don't forget to accept my answer if it worked. xInfinityBear 951 — 3mo
0
Also, mouse.KeyDown is deprecated. You should now always use UserInputService.InputBegan. youtubemasterWOW 2706 — 3mo
Ad

Answer this question