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

How detect key presses through serverscript?

Asked by 2 years ago

I have this localscript put in the character to detect when the key "v" is pressed. There are a plethora of problems it causes, other players can't see the change in skin color, and the main part of the script won't work either. It might be because i put it in the character and not player, how do I detect key presses in a serverscript and not localscript?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local char = script.Parent
local inputservice = game:GetService("UserInputService")

mouse.KeyDown:connect(function(key)
    if key == "v" then
        if char.Role.Value == "Vampire" and char.BeastMode.Value == false then
            char.BeastMode.Value = true
            char.Head.Color = Color3.new(0.74902, 0.74902, 0.74902)
            char.Torso.Color = Color3.new(0.74902, 0.74902, 0.74902)
            char["Left Arm"].Color = Color3.new(0.74902, 0.74902, 0.74902)
            char["Left Leg"].Color = Color3.new(0.74902, 0.74902, 0.74902)
            char["Right Arm"].Color = Color3.new(0.74902, 0.74902, 0.74902)
            char["Right Leg"].Color = Color3.new(0.74902, 0.74902, 0.74902)

        end
        print("Beast Mode!")
        end
        end)

1 answer

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

You can only detect user input through local scripts as they are running on the player's computer; unlike the server, which has no relation with any of the clients.

However, there's an easy way around this, and that's by using a Remote Event.

A Remote Event allows communication between the server and the client, providing a one-way message allowing Server Scripts to call code in LocalScripts and vice-versa. Here is the documentation if you'd like to read more about Remote Events.

It's also recommended to use UserInputService rather than the KeyDown event, as the KeyDown event is deprecated.


Local Script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local char = script.Parent
local inputservice= game:GetService("UserInputService")

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

inputservice.InputBegan:Connect(function(Input, GameProcessed)
    if GameProcessed then return end

    local key = Input.KeyCode
    if key == Enum.KeyCode.V then
        if char.Role.Value == "Vampire" and char.BeastMode.Value == false then
            RemoteEvent:FireServer() -- Pass parameters if you need
            print("Beast Mode!")
        end
    end
end)

Server Script

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(player)

    local char = player.Character

    if char.Role.Value == "Vampire" and char.BeastMode.Value == false then
        char.BeastMode.Value = true
        char.Head.Color = Color3.new(0.74902, 0.74902, 0.74902)
        char.Torso.Color = Color3.new(0.74902, 0.74902, 0.74902)
        char["Left Arm"].Color = Color3.new(0.74902, 0.74902, 0.74902)
        char["Left Leg"].Color = Color3.new(0.74902, 0.74902, 0.74902)
        char["Right Arm"].Color = Color3.new(0.74902, 0.74902, 0.74902)
        char["Right Leg"].Color = Color3.new(0.74902, 0.74902, 0.74902)
    end
end)

Ad

Answer this question