Hello, I’m trying to make a character bop to a beat while there’s no input, I tried using a while true do but it ended up crashing my script, I’m really stuck, can anyone help me fix this?
while idle == true do
script.Parent.Image = "rbxassetid://121803704513390"
wait(1)
script.Parent.Image = "rbxassetid://126828287438911"
wait(1)
end
FYI: It does work but once it starts playing, clicking (Main form of input) doesn’t work.
In how many places are you setting idle to true?
If it’s just one place, I would recommend putting the while loop directly after the line of code that sets idle to true
One way I’ve thought of doing this is just by having a simple loop and a variable that signifies the last time you’ve pressed something. In the loop, it detects that if you haven’t pressed anything for a set amount of seconds, it would handle the idling stuff.
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local lastInput = os.clock() -- last time you've pressed something
local timeUntilIdle = 5 -- how long it takes for you to become idle
local loopStarted = false
local stopFunction = nil
local function startLoop()
if not loopStarted then
local stop = false
task.spawn(function()
while not stop do
script.Parent.Image = "rbxassetid://121803704513390"
task.wait(1)
script.Parent.Image = "rbxassetid://126828287438911"
task.wait(1)
end
end)
stopFunction = function()
stop = true
loopStarted = false
end
loopStarted = true
end
end
UserInputService.InputBegan:Connect(function()
lastInput = os.clock()
end)
RunService.RenderStepped:Connect(function()
if not loopStarted and os.clock() - lastInput > timeUntilIdle then
startLoop()
elseif loopStarted and os.clock() - lastInput <= timeUntilIdle then
stopFunction()
end
end)
This code should also work based off the code that you gave.
local current = 0
while true do
if idle == true then
if current == 0 then
script.Parent.Image = "rbxassetid://121803704513390"
current = 1
else
script.Parent.Image = "rbxassetid://126828287438911"
current = 0
end
task.wait(1)
end
task.wait()
end