I want to loop my script so that two function I activate with a key on my keyboard can loop after they finish happening.
The issue is I haven’t used loops before and my attempts result in errors in my script. I’m not sure what to do to create the loop.
The solutions I have tried are to add “while true do” before my first function but it did not work.
Player = game.Players.LocalPlayer
a = script.Parent.Parent.Admins
Admins = {a.User1.Value,a.User2.Value,a.User3.Value,a.User4.Value,a.User5.Value}
for i,v in pairs (Admins) do
if Player.Name == v then
mouse = Player:GetMouse()
mouse.KeyDown:connect(function(key)
if key == "k" then
game.Workspace.KDHandler1.MoveTest:InvokeServer() --first function
wait(1)
game.Workspace.KDHandler1.MoveTest2:InvokeServer() ---second function
end
end)
end
end
Am I supposed to use “while true do” at all? If not what am I doing wrong?
Nevermind, figured it out! Lol. Hope this still helps someone else so I’ll leave this up but I am sure there is a better way to do this so if you find one let me know! And here’s the updated script:
Player = game.Players.LocalPlayer
a = script.Parent.Parent.Admins
Admins = {a.User1.Value,a.User2.Value,a.User3.Value,a.User4.Value,a.User5.Value}
for i,v in pairs (Admins) do
if Player.Name == v then
mouse = Player:GetMouse()
mouse.KeyDown:connect(function(key)
while true do
if key == "k" then
wait(1)
end
game.Workspace.KDHandler1.MoveTest:InvokeServer() --first function
wait(1)
game.Workspace.KDHandler1.MoveTest2:InvokeServer() ---second function
wait(1)
end
end)
end
end
I just had to add “if true then” before I defined the keybind and add another “wait(x)” after each function.
Nevermind again, doing this made my loop happen but then caused some lag? Almost like my lights move the way they should but then struggle to keep doing it. They basically get stuck and keep going, so I don’t know what is wrong now?
The following piece of code uses UserInputService. I understand that you’re using a local script.
This loop will stop as soon as the player releases the key “K” if you want the loop to continue forever remove the line UserInputService.InputEnded:Connect(InputEnded)
I recommend using Player.UserId instead of Player.Name (In case an admin changes username)
local Player = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Admins = {
574208891,-1,-2 -- Admins User Ids here
}
local KeyCode = Enum.KeyCode.K -- Your Key
local KeyDown = false -- Do not touch this
local LoopSpeed = 1 -- In Seconds
local function Input(input)
if UserInputService:IsKeyDown(KeyCode) then
if table.find(Admins, Player.UserId) then
KeyDown = true
else
print("Not an admin!")
end
end
end
local function InputEnded(input)
if KeyDown == true and input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == KeyCode then
KeyDown = false
end
end
UserInputService.InputBegan:Connect(Input)
UserInputService.InputEnded:Connect(InputEnded)
while true do
if KeyDown == true then
print("Key is down!")
-- Events here..
end
wait(LoopSpeed)
end
1 Like
Thank you so much! I’ll use this! c:
1 Like