Code not efficient enough?

I just made my first line of code as a developer! I just want to know. Does it seem to basic, to complexe? What did I do wrong? What could I do to make it more efficient?

Below is the code:

`> --Script location: game.StarterPlayer.StarterPlayerScripts

local UIS = game:GetService(“UserInputService”)
local player = game.Players.LocalPlayer
local PlayersService = game:GetService(“Players”)
local LocalPlayer = PlayersService.LocalPlayer

wait(5)

UIS.InputBegan:Connect(function(input,gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.R then
wait(1)
local Character = LocalPlayer.Character
print(“Player started pressing R”)
Character.Humanoid.Health = 0
end
end)`

  1. You seem to be switching between camelCase and PascalCase, pick one.
  2. You assume the player has a character, if they press r before spawning, the script will error.
1 Like

Why do you wait 5 seconds before the code runs, and then wait 1 second inside the InputBegan function? I would suggest getting rid of the waits. Also, make sure that the character actually exists inside of the InputBegan function.

Alright.

Ok. I will try to fix that. I have no idea how to. But the script was also made with making sure the player has a Character.

There is no need to have player and LocalPlayer because they are the same thing. You should either do local player = game.Players.LocalPlayer or

local Players = game:GetService("Players")
local player = Players.LocalPlayer

The wait was there because at first I was trying to use a while loop and just forgot to remove the wait. I will remove the first wait. The second one for some reason reduced lag in my studio when I pressed “R”.

Ok thank you, I will consider my use of variables next time I script.

1 Like