Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited by JesseSong 4 years ago

Code:

1game.Players.PlayedAdded:Connect(function(Player)
2local mouse = Player:GetMouse()
3    mouse.KeyDown:connect(function(h)
4 if h == "e" then
5   game.Workspace.Gravity = 0
6 end
7end)   

3 answers

Log in to vote
0
Answered by 4 years ago

Hi, you can use "UserInputService" for it. Here's an example how to code an input.

1local UIS = game:GetService("UserInputService")
2 
3UIS.InputBegan:Connect(function(input)
4    if input.KeyCode == Enum.KeyCode.e then
5        game:GetService("Workspace").Gravity = 0
6    end
7end)

Possible help understanding "UserInputService" is here. Have a great day.

Ad
Log in to vote
1
Answered by 4 years ago

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

01local UserInputService = game:GetService("UserInputService")
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
04 
05local Key = "E"
06 
07UserInputService.InputBegan:Connect(function(Input)
08    if Input.UserInputType == Enum.UserInputType.Keyboard then
09        if Input.KeyCode == Enum.KeyCode[Key] then
10            RemoteEvent:FireServer()
11        end
12    end
13end)

In a Script in ServerScriptService.

1local ReplicatedStorage = game:GetService("ReplicatedStorage")
2local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
3 
4RemoteEvent.OnServerEvent:Connect(function(Player)
5    workspace.Gravity = 0
6end)

There, the gravity has been changed, jump with your character and feel the difference.

Hope this helps

:)

0
i know i just want local Mel_pro1 5 — 4y
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. generalYURASKO 144 — 4y
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. Ziffixture 6913 — 4y
Log in to vote
0
Answered by 4 years ago

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

1local 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)

01local UserInputSerice = game:GetService("UserInputService")
02local player = game:GetService("Players").LocalPlayer
03 
04UserInputService.InputBegan:Connect(function(input)
05 
06    if input.UserInputType == Enum.UserInputType.Keyboard then
07 
08        if input.KeyCode == Enum.KeyCode.E then    
09 
10            -- Execute code in here
11 
12        end
13 
14    end
15 
16end)

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

Answer this question