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]
1 | game.ReplicatedStorage.RemoteEvent 1. OnServerEvent:Connect( function () |
2 | script.Parent.Transparency = 0.5 |
3 | end ) |
Local script [Inside of StarterPlayerScript]
01 | local plr = game:GetService( "Players" ).LocalPlayer |
02 | local Mouse = plr:GetMouse() |
03 | local Key = Enum.KeyCode.E |
04 | local UserInputService = game:GetService( "UserInputService" ) |
05 | local input = UserInputService.InputBegan:Connect( function (input, gp) |
06 |
07 |
08 | Mouse.KeyDown:Connect( function (k) |
09 | if input.KeyCode = = Key then |
10 | if Mouse.Target and Mouse.Target:FindFirstChild( "item" ) then |
11 | game.ReplicatedStorage.RemoteEvent 1 :FireServer() |
12 | end |
13 | end |
14 | end ) |
15 | end ) |
Please Help!
01 | local plr = game:GetService( "Players" ).LocalPlayer |
02 | local Mouse = plr:GetMouse() |
03 | local cas = game:GetService( "ContextActionService" ) |
04 |
05 |
06 | local function onEPress(name, state, input) |
07 | if state = = Enum.UserInputState.Begin then |
08 | game.ReplicatedStorage.RemoteEvent 1 :FireServer() |
09 | end |
10 | end |
11 |
12 | 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)
Hello,
01 | local plr = game:GetService( "Players" ).LocalPlayer |
02 | local Mouse = plr:GetMouse() |
03 | local cas = game:GetService( "ContextActionService" ) |
04 | local 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.RemoteEvent 1 :FireServer() |
08 | end |
09 | end |
10 | end |
11 |
12 | cas:BindAction( "ePressed" , EPressed, false , Enum.KeyCode.E) -- basically when E is pressed, onEPress runs. |
fixed script.
Mouse.Keydown is deprecated
, it is also unneeded in the script
01 | local plr = game:GetService( "Players" ).LocalPlayer |
02 | local Mouse = plr:GetMouse() |
03 | local Key = Enum.KeyCode.E |
04 | local UserInputService = game:GetService( "UserInputService" ) |
05 |
06 | UserInputService.InputBegan:Connect( function (input) |
07 | if input.KeyCode = = Key then |
08 | if Mouse.Target and Mouse.Target:FindFirstChild( "item" ) then |
09 | game.ReplicatedStorage.RemoteEvent 1 :FireServer() |
10 | end |
11 | end |
12 | 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.