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]

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

Local script [Inside of StarterPlayerScript]

01local plr = game:GetService("Players").LocalPlayer
02local Mouse = plr:GetMouse()
03local Key = Enum.KeyCode.E
04local UserInputService = game:GetService("UserInputService")
05local input = UserInputService.InputBegan:Connect(function(input, gp)
06 
07 
08Mouse.KeyDown:Connect(function(k)
09    if input.KeyCode == Key then
10    if Mouse.Target and Mouse.Target:FindFirstChild("item") then
11     game.ReplicatedStorage.RemoteEvent1:FireServer()
12    end
13    end
14    end)
15end)

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
01local plr = game:GetService("Players").LocalPlayer
02local Mouse = plr:GetMouse()
03local cas = game:GetService("ContextActionService")
04 
05 
06local function onEPress(name, state, input)
07    if state == Enum.UserInputState.Begin then
08     game.ReplicatedStorage.RemoteEvent1:FireServer()
09    end
10   end
11 
12cas: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,

01local plr = game:GetService("Players").LocalPlayer
02local Mouse = plr:GetMouse()
03local cas = game:GetService("ContextActionService")
04local function EPressed(name,state,input)
05    if state == Enum.UserInputState.Begin then
06    if Mouse.Target and Mouse.Target:FindFirstChild("item") then
07     game.ReplicatedStorage.RemoteEvent1:FireServer()
08    end
09    end
10end
11 
12cas: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

01local plr = game:GetService("Players").LocalPlayer
02local Mouse = plr:GetMouse()
03local Key = Enum.KeyCode.E
04local UserInputService = game:GetService("UserInputService")
05 
06UserInputService.InputBegan:Connect(function(input)
07    if input.KeyCode == Key then
08        if Mouse.Target and Mouse.Target:FindFirstChild("item") then
09            game.ReplicatedStorage.RemoteEvent1:FireServer()
10        end
11    end
12end)

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