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

Keydown function wont work inside normal script?

Asked by 4 years ago
Edited 4 years 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 4 years ago
Edited 4 years 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 17 — 4y
0
No problem, happy to help. xInfinityBear 1777 — 4y
0
Don't forget to accept my answer if it worked. xInfinityBear 1777 — 4y
0
Also, mouse.KeyDown is deprecated. You should now always use UserInputService.InputBegan. youtubemasterWOW 2741 — 4y
Ad

Answer this question