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

I tried to script it so if i press "e" then it takes gravity away from you?

Code:



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)	

3 answers

✓ Accepted answer

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.

Now, programming.

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

:)

3 comments

0

i know i just want local

0

Local scripts cant edit things in the server, they can only change things for the player, this is prevent hackers from being able to edit local scripts (which they can do, because local scripts can be accessed by the client) and then start changing the game around.

0

LocalScripts are programs that operate on the ROBLOX (Client) Player Application on the user's device. This is why they have domestic control; exploiters can access them as they're situated on situated on their device.

(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.

Links -

https://youtu.be/cRu1EbcsJiQ -- Event Listeners

https://youtu.be/wCr5VXJ34T4 - Remote Events

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