game.Players.PlayedAdded:Connect(function(Player) local mouse = Player:GetMouse() mouse.KeyDown:connect(function(h) if h == "e" then game.Workspace.Gravity = 0 end end)
Hi, you can use "UserInputService" for it. Here's an example how to code an input.
local UIS = game:GetService("UserInputService") UIS.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.e then game:GetService("Workspace").Gravity = 0 end end)
Possible help understanding "UserInputService" is here. Have a great day.
I recommend that you use UserInputService to detect when a key on the keyboard is pressed, and as for the code, it is correct, but the changes made are local, so they do not affect other players outside yours.
To resolve this you must use a remoteEvent and a Script in ServerScriptService to make changes across the server.
First, create a RemoteEvent in ReplicatedStorage.
In a LocalScript
local UserInputService = game:GetService("UserInputService") local ReplicatedStorage = game:GetService("ReplicatedStorage") local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent") local Key = "E" UserInputService.InputBegan:Connect(function(Input) if Input.UserInputType == Enum.UserInputType.Keyboard then if Input.KeyCode == Enum.KeyCode[Key] then RemoteEvent:FireServer() end end end)
In a Script in ServerScriptService.
local ReplicatedStorage = game:GetService("ReplicatedStorage") local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent") RemoteEvent.OnServerEvent:Connect(function(Player) workspace.Gravity = 0 end)
There, the gravity has been changed, jump with your character and feel the difference.
Hope this helps
(To preface this I want to say that I don't know how to turn off gravity for a specific player, however, there were a few big problems in what your coding, so im trying to address thoes and im gonna show you how to detect when a player presses a button)
Ok, to start your gonna need to get "User Input Service", its one of Roblox's base interactions so its easy to use. However, keep in mind UserInputService can only be called from a local script because it is referring to a specific player because a server script that runs on the server has no reason to have a UserInputService cause the server will never be inputting UserInput (I hope that makes sense)
local UserInputService = game:GetService("UserInputService")
Secondly, your gonna want to set up your EventListener, if you don't know (which I'm assuming you don't cause you did not use one) Event listeners are a way to tell Roblox to look for a specific thing to happen, so when it does, you can perform specific actions (I also hope that makes sense) In this case you're looking for the player to press "E". (so far the code is as follows)
local UserInputSerice = game:GetService("UserInputService") local player = game:GetService("Players").LocalPlayer UserInputService.InputBegan:Connect(function(input) if input.UserInputType == Enum.UserInputType.Keyboard then if input.KeyCode == Enum.KeyCode.E then -- Execute code in here end end end)
Now, I cant explain all of this, so instead, I will direct you to a very helpful few videos that should explain some of your questions hopefully. In addition, I want to let you know that your gonna need to use remote events in order to do this, once again I can't explain all the functions of remote events but they are very important so I would suggest that they are one of the next things you learn.
https://youtu.be/cRu1EbcsJiQ -- Event Listeners
https://youtu.be/wCr5VXJ34T4 - Remote Events