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

When i press the key nothing happens and there are no errors in the script, any help?

Asked by
Dec_ade 47
4 years ago
01local UserInputService = game:GetService("UserInputService")
02local Player = game.Players.LocalPlayer
03 
04local CombatEnabled = false
05 
06UserInputService.InputBegan:Connect(function(Input,IsTyping)
07    if not IsTyping then
08        if Input.KeyCode == Enum.KeyCode.E then
09            CombatEnabled = true
10        end
11    end
12end)
13 
14spawn(function()
15    while CombatEnabled == true do
View all 32 lines...

This local script is inside the starterpack, and there are no errors in the output.

1 answer

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

Hello there.

Issue:

The while loop would immediately stop CobatEnabled would be false so the while loop wouldn't run.

Fix:

Use a while true do loop checking if combat is enabled.

Improvments:

  1. Don't use break on the while loop if the combat isn't enabled.

  2. Use coroutines instead of spawn as spawn has a built-in delay.

  3. Use game:GetService() with the Players service.

Fixed code:

01local UserInputService = game:GetService("UserInputService")
02local Player = game:GetService("Players").LocalPlayer
03 
04local CombatEnabled = false
05 
06UserInputService.InputBegan:Connect(function(Input,IsTyping)
07    if not IsTyping then
08        if Input.KeyCode == Enum.KeyCode.E then
09            CombatEnabled = true
10        end
11    end
12end)
13 
14coroutine.wrap(function()
15 
View all 26 lines...

Please accept and upvote this answer if it helps!

Ad

Answer this question