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

Repeating For-loop makes the keybind script blink?

Asked by 6 years ago
01--LocalScript inside StarterPlayerScripts
02player = game.Players.LocalPlayer
03 
04local sp = player:WaitForChild('PlayerGui'):WaitForChild('Keybind'):WaitForChild('ImageButton')
05 
06local UIS = game:GetService("UserInputService")
07 
08UIS.InputBegan:Connect(function(input,gameProcessed)
09 
10    if input.UserInputType == Enum.UserInputType.Keyboard  then
11        if input.KeyCode == Enum.KeyCode.E then
12            if UIS:GetFocusedTextBox() == nil then
13 
14                for i,v in pairs(game.Workspace.Stove["Gas"]:GetChildren()) do
15 
16                    local mag = (v.Parent.Union.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude
17 
18                    if mag <= 15 then
19 
20                        game.ReplicatedStorage.Stove:FireServer("Gas")
21 
22                    end
23                end
24            end
25        end
26    end
27end)
28 
29while wait() do
30 
31    for i,v in pairs(game.Workspace.Stove["Gas"]:GetChildren()) do
32        local mag = (v.Parent.Union.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude
33        local WSP = game.Workspace.CurrentCamera:WorldToScreenPoint(v.Parent.Union.Position)
34 
35        if mag <= 15 then
36            sp.Visible = true
37 
38            sp.Position = UDim2.new(0,WSP.X,0,WSP.Y)
39 
40            sp.TextLabel.Text = "Open 'Stove'"
41 
42        end
43    end
44end

This is my keybind script. If you go near it, an ImageButton ‘E’ will appear, and if you click this, it will fire an remoteEvent. But I see a problem that is the button is blinking(because the while loop and the for loop, and it means there is a delay). If you just add one more line of a script then the blinking period(the sp.Visible = false) increases.
What do I do?
[There are tons of stoves in the folder]

Answer this question