Good afternoon, please help me optimize the code, as I have already run out of ideas.
The fact is that I wrote a code for rotating an object when pressing keys. When checking in the Roblox Studio itself, everything works fine, without bugs, without errors. But the problem lies when testing in the Roblox itself.
local UserInput = game:GetService("UserInputService")
local lastTouchE = tick()
local lastTouchQ = tick()
local R = 0
local start = nil
UserInput.InputBegan:Connect(function(i,e)
if e then return end
if i.KeyCode == Enum.KeyCode.E then
R = R - 2
local timeSinceLastTouch = tick() - lastTouchE
lastTouchE = tick()
if timeSinceLastTouch >= 0.2 then
start = true
repeat
if start == true then
R = R - 2
wait()
else
start = false
end
until start == false
else
start = false
end
end
if i.KeyCode == Enum.KeyCode.Q then
R = R + 2
local timeSinceLastTouch = tick() - lastTouchQ
lastTouchQ = tick()
if timeSinceLastTouch >= 0.2 then
start = true
repeat
if start == true then
R = R + 2
wait()
else
start = false
end
until start == false
else
start = false
end
end
end)
UserInput.InputEnded:Connect(function (i,e)
if i.KeyCode == Enum.KeyCode.E then
wait()
start = false
end
if i.KeyCode == Enum.KeyCode.Q then
wait()
start = false
end
end)
The whole problem is that when you hold down the part key, it should spin, and as I explained above, everything works flawlessly in Roblox Studio. There are no errors in Roblox, but with quick clicks and various manipulations, the part just starts spinning without stopping when the player does not press the key, I need the part to spin only when the player holds down the key or just presses it. I’ve tried many variations of scripts, this one does the best job so far, but it’s still bad.
Heyo! It looks like you have this problem because you’re using the same flag (start) for both the KeyCode.E and KeyCode.Q sections, so one key press can overwrite the others state. You should try using separate flags
Lmk if this helped
I tried to do as you said, the script started to look like this, but the part still starts spinning even after I stop pressing the key.
local UserInput = game:GetService("UserInputService")
local lastTouchE = tick()
local lastTouchQ = tick()
local R = 0
local startE = nil
local startQ = nil
UserInput.InputBegan:Connect(function(i,e)
if e then return end
if i.KeyCode == Enum.KeyCode.E then
R = R - 2
local timeSinceLastTouch = tick() - lastTouchE
lastTouchE = tick()
if timeSinceLastTouch >= 0.2 then
startE = true
repeat
if startE == true and startQ == false then
R = R - 2
wait()
else
startE = false
end
until startE == false
else
startE = false
end
end
if i.KeyCode == Enum.KeyCode.Q then
R = R + 2
local timeSinceLastTouch = tick() - lastTouchQ
lastTouchQ = tick()
if timeSinceLastTouch >= 0.2 then
startQ = true
repeat
if startQ == true and startE == false then
R = R + 2
wait()
else
startQ = false
end
until startQ == false
else
startQ = false
end
end
end)
UserInput.InputEnded:Connect(function (i,e)
if i.KeyCode == Enum.KeyCode.E then
wait()
startE = false
end
if i.KeyCode == Enum.KeyCode.Q then
wait()
startQ = false
end
end)
as I understand it, this is due to the fact that the start variable changes to false either too late or remains true.
I tried optimizing it, dont know if it works with the part where the rotation is processed and if its what you wanted
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local rotateLeft = false
local rotateRight = false
local R = 0
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
rotateLeft = true
elseif input.KeyCode == Enum.KeyCode.Q then
rotateRight = true
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
rotateLeft = false
elseif input.KeyCode == Enum.KeyCode.Q then
rotateRight = false
end
end)
RunService.RenderStepped:Connect(function()
if rotateLeft then
R = R - 2
end
if rotateRight then
R = R + 2
end
end)
I’ve had a similar issue where I need a function to only work when a specific key was held so I made a table that would add a specific key when its pressed and removed it when the player releases it
local UserInput = game:GetService("UserInputService")
local lastTouchE = tick()
local lastTouchQ = tick()
local R = 0
local Pressed = {}
UserInput.InputBegan:Connect(function(i,e)
if e then return end
if i.KeyCode == Enum.KeyCode.E then
R = R - 2
local timeSinceLastTouch = tick() - lastTouchE
lastTouchE = tick()
if timeSinceLastTouch >= 0.2 then
Pressed = {}
Pressed[i.KeyCode] = Enum.KeyCode.E
repeat
R = R - 2
wait()
until Pressed[i.Keycode] == Enum.KeyCode.E
end
end
if i.KeyCode == Enum.KeyCode.Q then
R = R + 2
local timeSinceLastTouch = tick() - lastTouchQ
lastTouchQ = tick()
if timeSinceLastTouch >= 0.2 then
Pressed = {}
Pressed[i.KeyCode] = Enum.KeyCode.Q
repeat
R = R + 2
wait()
until Pressed[i.KeyCode] == Enum.KeyCode.Q
end
end
end)
UserInput.InputEnded:Connect(function (i,e)
if i.KeyCode == Enum.KeyCode.E then
Pressed[i.KeyCode] = nil
end
if i.KeyCode == Enum.KeyCode.Q then
Pressed[i.KeyCode] = nil
end
end)
Unfortunately, the result is the same, and after I stop pressing the key, the part continues to rotate.
I know what the problem is, but I don’t know how to solve it.
UserInput.InputEnded:Connect(function (i,e)
if i.KeyCode == Enum.KeyCode.E then
Pressed[i.KeyCode] = nil
end
if i.KeyCode == Enum.KeyCode.Q then
Pressed[i.KeyCode] = nil
end
end)
The problem is that when I release the key, the table either does not reset to zero, or it turns positive again and the cycle does not stop. It would be convenient if we could check in a loop whether the player continues to press this button or not.
I’ve solved the problem, my script looked like this:
UserInput.InputEnded:Connect(function (i,e)
if i.KeyCode == Enum.KeyCode.E then
wait()
Pressed = {}
end
if i.KeyCode == Enum.KeyCode.Q then
wait()
Pressed = {}
end
end)
UserInput.InputBegan:Connect(function (i,e)
if e then return end
local PlrData = Data:InvokeServer()
if i.UserInputType == Enum.UserInputType.MouseButton1 and NewFurniture then
if Use and NewFurniture and Step and Spawner then
if #workspace:WaitForChild("PlayerZone")[game.Players.LocalPlayer.Name]:WaitForChild("Furnitures"):GetChildren() < HousesModule[PlrData.ActiveHouse].Max then
Step = false
SpawnFurniture:FireServer(script.Parent.Name, NewFurniture.PrimaryPart.CFrame, script.Parent)
anim:Stop()
FurnitureDestroy()
end
end
end
if i.KeyCode == Enum.KeyCode.E then
Q = Q - 2
local timeSinceLastTouch = tick() - lastTouchE
lastTouchE = tick()
if timeSinceLastTouch >= 0.2 then
Pressed = {}
Pressed[i.KeyCode] = Enum.KeyCode.E
task.wait(0.2)
if Pressed[i.KeyCode] == Enum.KeyCode.E then
repeat
Q = Q - 2
wait()
until Pressed[i.KeyCode] == nil
end
end
end
if i.KeyCode == Enum.KeyCode.Q then
Q = Q + 2
local timeSinceLastTouch = tick() - lastTouchQ
lastTouchQ = tick()
if timeSinceLastTouch >= 0.2 then
Pressed = {}
Pressed[i.KeyCode] = Enum.KeyCode.Q
task.wait(0.2)
if Pressed[i.KeyCode] == Enum.KeyCode.Q then
repeat
Q = Q + 2
wait()
until Pressed[i.KeyCode] == nil
end
end
end
end)
I just separated the rotation function from the mouse click, and everything started working as it should. Thank you so much for helping me!