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

Does Keybind script need Remote Event?

Asked by 6 years ago

I have been trying to make a keybind Event but fell to because nothing I tried worked, why?

In the script below I am using Remote Event to connect to the keybind script. Tell me if I am doing it wrong.

script [Inside of a brick]

game.ReplicatedStorage.RemoteEvent1.OnServerEvent:Connect(function()
    script.Parent.Transparency = 0.5
end)

Local script [Inside of StarterPlayerScript]

local plr = game:GetService("Players").LocalPlayer
local Mouse = plr:GetMouse() 
local Key = Enum.KeyCode.E
local UserInputService = game:GetService("UserInputService")
local input = UserInputService.InputBegan:Connect(function(input, gp)


Mouse.KeyDown:Connect(function(k)
    if input.KeyCode == Key then
    if Mouse.Target and Mouse.Target:FindFirstChild("item") then
     game.ReplicatedStorage.RemoteEvent1:FireServer()
    end
    end
    end)
end)

Please Help!

0
Why is there an end) on the 14th line? Tymberlejk 143 — 6y

3 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago
local plr = game:GetService("Players").LocalPlayer
local Mouse = plr:GetMouse() 
local cas = game:GetService("ContextActionService") 


local function onEPress(name, state, input)
    if state == Enum.UserInputState.Begin then
     game.ReplicatedStorage.RemoteEvent1:FireServer()
    end
   end

cas:BindAction("ePressed", onEPress, false, Enum.KeyCode.E) -- basically when E is pressed, onEPress runs.


It might not work as I might have messed up (I just discovered context action service yesterday)

Ad
Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

Hello,

local plr = game:GetService("Players").LocalPlayer
local Mouse = plr:GetMouse()
local cas = game:GetService("ContextActionService")
local function EPressed(name,state,input)
    if state == Enum.UserInputState.Begin then
    if Mouse.Target and Mouse.Target:FindFirstChild("item") then
     game.ReplicatedStorage.RemoteEvent1:FireServer()
    end
    end
end

cas:BindAction("ePressed", EPressed, false, Enum.KeyCode.E) -- basically when E is pressed, onEPress runs.

fixed script.

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Mouse.Keydown is deprecated, it is also unneeded in the script

local plr = game:GetService("Players").LocalPlayer
local Mouse = plr:GetMouse()
local Key = Enum.KeyCode.E
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Key then
        if Mouse.Target and Mouse.Target:FindFirstChild("item") then
            game.ReplicatedStorage.RemoteEvent1:FireServer()
        end
    end
end)

I also forgot to mention, a keybind script is necessary to recieve player input. Since the server is unable to read client keyboard/mouse information a remote is necessary. If you were to modify the part locally, than it would only replicate to the client themselves. Meaning other players wouldn't be able to see any changes. If that is what you we're aiming for, than the remote is not necessary.

Answer this question