How to change model orientation while its changing with runservice

how can i change a model orientation while its getting changed by a runservice function

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local modulemouse = require(game.ReplicatedStorage.MouseModule.MouseModule).new() -- just a mouse module
local mouse = plr:GetMouse()

local rs = game:GetService("RunService")

local Window = workspace.LongWindow

mouse.TargetFilter = mouse

rs.RenderStepped:Connect(function()
	
	local Hit, Target:Instance, Normal = modulemouse:ProjectMouseRay({Window, char}) -- makes a ray that returns the cframe (hit) and instance (target) and the normal

	if Hit and Target and Normal then -- just checking if nothing is nil
		
		Window:PivotTo(CFrame.lookAt(Hit.Position, Hit.Position + Normal)) -- actually aligning the window to the part it the ray hit by using normal
		
	end
		
end)

local uis = game:GetService('UserInputService')

uis.InputBegan:Connect(function(key)
	
	if key.KeyCode == Enum.KeyCode.E then

		local rotation = CFrame.Angles(0, math.rad(45), 0)

		if Window:IsA("Model") then

			local modelCFrame = Window:GetPivot()
			Window:PivotTo(modelCFrame * rotation) -- changing it but it dosent change because the runservice is overriding it

		end
			
	elseif key.KeyCode == Enum.KeyCode.Q then
		
		local rotation = CFrame.Angles(0, math.rad(-45), 0)

		if Window:IsA("Model") then

			local modelCFrame = Window:GetPivot()
			Window:PivotTo(modelCFrame * rotation) -- changing it but it dosent change because the runservice is overriding it

		end
		
	end
	
end)


the issue here is that when you press E to change the orientation it gets changed but the runservice quickly alters it by the normal angle, so how can i make the rotation work with the normal altering the model?

any help will be appreciated

1 Like

I may be wrong but I think inputs run before render stepped in a frame. So maybe using task.defer to rotate after render stepped would work

1 Like

is it possible to show me the code please

1 Like
if Window:IsA("Model") then
	task.defer(function()
        local modelCFrame = Window:GetPivot()
        Window:PivotTo(modelCFrame * rotation)
    end)
end
1 Like

i apologise for the late responses, but it sadly did not work heres the script i put:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local modulemouse = require(game.ReplicatedStorage.MouseModule.MouseModule).new() -- just a mouse module
local mouse = plr:GetMouse()

local rs = game:GetService("RunService")

local Window = workspace.LongWindow

rs.RenderStepped:Connect(function()
	
	local Hit, Target:Instance, Normal = modulemouse:ProjectMouseRay({Window, char}) -- makes a ray that returns the cframe (hit) and instance (target) and the normal
	
	if Hit and Target and Normal then -- just checking if nothing is nil
		
		local X,Y,Z = Window:GetPivot():ToOrientation()
		
		Window:PivotTo(CFrame.lookAt(Hit.Position, Hit.Position + Normal))
		
	end
		
end)

local InputBegan = game.UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then

		local rotation = CFrame.Angles(0, math.rad(45), 0)

		if Window:IsA("Model") then
			task.defer(function()
				local modelCFrame = Window:GetPivot()
				Window:PivotTo(modelCFrame * rotation)
			end)
		end

	elseif input.KeyCode == Enum.KeyCode.Q and not gameProcessedEvent then

		local rotation = CFrame.Angles(0, math.rad(-45), 0)

		if Window:IsA("Model") then
			task.defer(function()
				local modelCFrame = Window:GetPivot()
				Window:PivotTo(modelCFrame * rotation)
			end)
		end

	end
end)

Try this then

if Window:IsA("Model") then
	rs.Heartbeat:Wait()
    local modelCFrame = Window:GetPivot()
	Window:PivotTo(modelCFrame * rotation)
end