Simply put, I want it so when I press R, it rotates with a speed of 0.5.
However, everytime I press R, the rotation speed gets faster, which is unwanted.
I’ve tried setting the 0.5 to a variable and reusing it in hopes of fixing, but nope. Didn’t work.
heartBeat = RunService.Heartbeat:Connect(function()
local mousePosition = UIS:GetMouseLocation()
local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
rotationSpeed = 0.07
--local mouseRay = mouse.UnitRay
local hit = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, Raycastparams)
if hit then
UIS.InputBegan:Connect(function(input)
if input.keyCode == Enum.KeyCode.R then
rKeyPressed = true
while rKeyPressed do
wait()
yOrientation = yOrientation + 0.5
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.keyCode == Enum.KeyCode.R then
rKeyPressed = false
end
end)
newAngleCFrame = CFrame.Angles(math.rad(xOrientation), math.rad(yOrientation), math.rad(zOrientation))
newCFrame = CFrame.new(hit.Position.X, hit.Position.Y, hit.Position.Z)
placementObject:PivotTo(newCFrame * newAngleCFrame)
end
--placementObject.CFrame = mouse.Hit
end)
Each RunService.Heartbeat, you are creating a new connection. Not only does this eat a lot into the memory, it also means each time you give input it will run the code to increase the angle things however many times you have connected it. You should move these connections outside of the RunService.Heartbeat connection.
RunService.Heartbeat is known as an RBXScriptSignal. It’s a fancy way of saying it signals to a script when something happens, e.g. .Touched when a part is touched. We can use the :Connect method of the signal to hook it up to the given code. This means whenever that signal fires, it will run the connected code, and is known as an RBXScriptConnection.
:Connect will make a new connection each time, regardless of whether it has already been connected to something else. And, since RunService.Heartbeat fires every 0.001 seconds, each time it is fired, you are hooking it up to the same code again. So, by the time you reach 0.001 seconds, you have hooked up the same code to the event 10 times. This means each time the signal fires, the same code will be run 10 times.
The first one will never “end” - it will still be consuming memory. You need to disconnect it with :Disconnect(), or you could use :Once() to connect only once.
You can use your rKeyPressed variable.
This method should minimise the amount of connections used:
local uis = game:GetService("UserInputService")
local rKeyPressed = false
local function connect()
while rKeyPressed do
--do your raycasting code and other stuff here
task.wait(0.0001) --same interval as heartbeat
end
end
local function start(input:InputObject, processed:boolean)
if processed then return nil end
if input.KeyCode == Enum.KeyCode.R then
rKeyPressed = true
connect()
end
end
local function finish(input:InputObject, processed:boolean)
if processed then return nil end
if input.KeyCode == Enum.KeyCode.R then
rKeyPressed = false
end
end
uis.InputBegan:Connect(start)
uis.InputEnded:Connect(finish)