Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Does Keybind script need Remote Event?

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!

1 comment

0

Why is there an end) on the 14th line?

3 answers

✓ Accepted answer
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)

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.