CFrame not rotating

local mouse = game.Players.LocalPlayer:GetMouse()
local off = false

local userInputService = game:GetService("UserInputService")





script.Parent.Equipped:Connect(function(toolMouse)
	if mouse ~= nil then
			local clonedBrick = game.ReplicatedStorage.MPIModel:Clone()
		clonedBrick.PrimaryPart.CFrame = clonedBrick.PrimaryPart.CFrame * CFrame.Angles(0,0,math.rad(90))
			mouse.TargetFilter = clonedBrick	
--		clonedBrick.Transparency = .5
		--clonedBrick.CanCollide = false
		clonedBrick.Parent = workspace
			while mouse ~= nil do
			local positiongoal = mouse.Hit.p + Vector3.new(0, 0.08, 0)
				clonedBrick:SetPrimaryPartCFrame(CFrame.new(positiongoal))
				wait()
			if off then
				clonedBrick:Destroy()
				off = false
			break
			end
		end
		userInputService.InputBegan:Connect(function(input, inGui)
		local key = input.KeyCode -- Here we get the Enum KeyCode which was pressed
		if key == Enum.KeyCode.R then
				print ("key pressed")
				clonedBrick.PrimaryPart.CFrame = clonedBrick.PrimaryPart.CFrame * CFrame.Angles(0,0,math.rad(20))  -- CFrame.Angles uses radians so we need to convert 90 using math.rad
			end	
		end)
	end
	end)
		


script.Parent.Activated:Connect(function()
	script.Parent.GotGunEvent:FireServer()
	print("got gun event fired")
	local BrickToPlace = game.ReplicatedStorage.MPIModel:Clone()
	local pos = mouse.Hit.p + Vector3.new(0, 0.08, 0)
	BrickToPlace.PrimaryPart.CFrame = BrickToPlace.PrimaryPart.CFrame * CFrame.Angles(0,0,math.rad(90))
	BrickToPlace:SetPrimaryPartCFrame(CFrame.new(pos))
	script.Parent.AddBlock:FireServer(pos)
	off = true
	wait (0.1)
	BrickToPlace:Destroy()
	script.parent:Destroy()
end)

script.Parent.Unequipped:Connect(function()
	off = true
end)

You can see near the bottom of the first code block that I am trying to rotate with R - nothing actually happens when I press R - nothing appears in output either (including that print)

If anyone knows how to fix this please help

What is “inGui” referencing?

userInputService.InputBegan:Connect(function(input, inGui) 

I used parts of code from this and tried to make it work

The rotation part is the only string of code I used from this

Is this being played locally or on the server? User Input Service only works locally iirc

It is local, any other idea as to why it isn’t working?

It looks like you might be trying to clone the part from Replicated Storage locally, prior to the FireServer call?

The way this works is that it displays a transparent model to show where you are placing it - that is of course local

I don’t understand how this would affect the rotation…?

Where exactly is the script? StarterPlayerScripts? Nevermind, i saw the LocalPlayer

image

Would it be easier to have the file? (I don’t want to have it publicly shared however so I would want to PM it)

EDIT: I am going to bed now but if anyone else can assist without having the file on hand right now please do - if you think you could fix it with more context then PM me (or however that works)

Okay, whenever you’re back can you check if the script is Actually detecting any input?

you can do this by applying a Print before/after

local key = input.KeyCode

Try removing the key variable and just use if input.KeyCode == Enum.KeyCode.R then and see if that works.

This is why

There are quite a few problems with your script. If you include a while true loop the thread will not pass this while loop. Mouse is always persistent. It will always exist

To fix you have go use an stepping connection or move th while loop below the Input began event.

For example RenderStepped or BindToRenderStepped
or even better use the Mouse.Move event.

https://developer.roblox.com/en-us/api-reference/event/Mouse/Move

Id be happy to give you more point on how you can improve with this.

Yeah I don’t complete understand - would you be able to explain?

EDIT: tried to implement it into code

Script.Parent.Equipped:Connect(function(toolMouse)
	if mouse.move then
			local clonedBrick = game.ReplicatedStorage.MPIModel:Clone()
		clonedBrick.PrimaryPart.CFrame = clonedBrick.PrimaryPart.CFrame * CFrame.Angles(0,0,math.rad(90))
			mouse.TargetFilter = clonedBrick	
--		clonedBrick.Transparency = .5
		--clonedBrick.CanCollide = false
		clonedBrick.Parent = workspace
			while mouse.move do
			local positiongoal = mouse.Hit.p + Vector3.new(0, 0.08, 0)
				clonedBrick:SetPrimaryPartCFrame(CFrame.new(positiongoal))
			if off then
				clonedBrick:Destroy()
				off = false
			end
		end
		userInputService.InputBegan:Connect(function(input, inGui)
			--local key = input.KeyCode -- Here we get the Enum KeyCode which was pressed
			if input.KeyCode == Enum.KeyCode.R then
				print ("key pressed")
				clonedBrick.PrimaryPart.CFrame = clonedBrick.PrimaryPart.CFrame * CFrame.Angles(0,0,math.rad(20))  -- CFrame.Angles uses radians so we need to convert 90 using math.rad
			end	
		end)
	end
	end)

This ends up freezing now but I don’t see how? It isn’t in a loop any longer…

(when it included the break and wait(0) event it still worked)

Also to update - it wasn’t detecting R (when wait(0) and and break were still in place)

UPDATE:

On the official devforum page it says

Note, this event is fired when the mouse’s position is updated, therefore it will fire repeatedly whilst being moved.

So I now understand why it freezes and lags out - but I don’t know how I could fix this problem - you mentioned other events.

Even after reading over RenderStepped and BindToRenderStepped I am not very sure how they would be implemented

Also just to update this - there was already a print function and no, it isn’t detecting anything - look at Agent’s post as to why this is

Tried that as well but didn’t work for the same reason as in Agent’s post

The while loop is the problem.

Mouse.Move is an event. It isn’t a condition. It always exist so it is basically the same as doing while true do. This is how it should be used

Mouse.Move:Connect(function()
    -- Code when mouse moves.
end)

To be honest, it is actually much better to use UserInputService for this type of stuff anyways.

UserInputService.InputChanged:Connect(function(Input: InputObject, GPE)
    if GPE then return end
    if Input.UserInputType == Enum.UserInputType.MouseMovement then
        -- Code here for mouse moved
    end
end)

Also I see that you might want to use ContextActionService instead of UIS for this. The reason why is because you only need the R keybind when you press R

https://developer.roblox.com/en-us/api-reference/class/ContextActionService

I would structure it like this:

local ContextAction = game:GetService("ContextActionService")

local MouseMove;
Tool.Equipped:Connect(function(Mouse)
    -- Initialize your part/clone it to the workspace
    MouseMove = Mouse.Move:Connect(function() -- This code is fire when player moves mouse
        -- Code to update part to mouse position
    end)
    ContextAction:BindAction("RotatePart",function(ActionName, UserInputState, InputObject) -- bind the action when we equip tool
        -- Rotate part Code
    end,
    false, -- false or true up to you whether or not you want buttons
    Enum.KeyCode.R
end)
Tool.Unequipped:Connect(function()
    MouseMove:Disconnect() -- it is VERY IMPORTANT to disconnect events that you are not using or you will end up having problems with performance
    Context:UnbindAction("RotatePart") -- unbind the action when we unequip
end)

Hopefully you got something from this.

I recommend you look at the documentation for the objects if you do not understand.
Also, I don’t recommend looking at free model script due to there being a possibility of bad code. It is much better to look on the Devhub for code there.

https://developer.roblox.com/en-us/api-reference/class/ContextActionService
https://developer.roblox.com/en-us/api-reference/class/Mouse

image

image

image

Fixed the statement spans multiple lines but it is still angry about the bracket

Confused if this was just an example which you just typed out without checking in a script but I looked at ContextActionService and it didn’t seem to be laid out this way either.

Whoops. I forgot to indent and I didn’t close the bracket…

Tool.Equipped:Connect(function(Mouse)
    -- Initialize your part/clone it to the workspace
    MouseMove = Mouse.Move:Connect(function() -- This code is fire when player moves mouse
        -- Code to update part to mouse position
    end)
    ContextAction:BindAction(
        "RotatePart",
        function(ActionName, UserInputState, InputObject) -- bind the action when we equip tool
            
        end,
        false, 
        Enum.KeyCode.R
    )
end)

I usually do things by memory and then Roblox’s Auto completer usually fixes the indents and closing brackets.

You could make a function if you wanted to. I usually write like this for speed.

local mouse = game.Players.LocalPlayer:GetMouse()
local off = false

local userInputService = game:GetService("UserInputService")

local ContextAction = game:GetService("ContextActionService")

local MouseMove;
script.parent.Equipped:Connect(function(Mouse)
	-- Initialize your part/clone it to the workspace
	local clonedBrick = game.ReplicatedStorage.MPIModel:Clone()
	clonedBrick.PrimaryPart.CFrame = clonedBrick.PrimaryPart.CFrame * CFrame.Angles(0,0,math.rad(90))
	mouse.TargetFilter = clonedBrick	
	clonedBrick.Parent = workspace
	
	MouseMove = Mouse.Move:Connect(function() -- This code is fire when player moves mouse
		-- Code to update part to mouse position
			local positiongoal = mouse.Hit.p + Vector3.new(0, 0.08, 0)
			clonedBrick:SetPrimaryPartCFrame(CFrame.new(positiongoal))
				clonedBrick:Destroy()
	end)
	
	ContextAction:BindAction(
		"RotatePart",
		function(ActionName, UserInputState, InputObject) -- bind the action when we equip tool

		end,
		true, 
		Enum.KeyCode.R
	)
end)

script.parent.Unequipped:Connect(function()
	MouseMove:Disconnect() -- it is VERY IMPORTANT to disconnect events that you are not using or you will end up having problems with performance
	ContextAction:UnbindAction("RotatePart") -- unbind the action when we unequip
end)


script.Parent.Activated:Connect(function()
	script.Parent.GotGunEvent:FireServer()
	print("got gun event fired")
	local BrickToPlace = game.ReplicatedStorage.MPIModel:Clone()
	local pos = mouse.Hit.p + Vector3.new(0, 0.08, 0)
	BrickToPlace.PrimaryPart.CFrame = BrickToPlace.PrimaryPart.CFrame * CFrame.Angles(0,0,math.rad(90))
	BrickToPlace:SetPrimaryPartCFrame(CFrame.new(pos))
	script.Parent.AddBlock:FireServer(pos)
	off = true
	wait (0.1)
	BrickToPlace:Destroy()
	script.parent:Destroy()
end)

script.Parent.Unequipped:Connect(function()
	off = true
end)

image

So as you can see in this code I changed it to fit but now I am getting a problem with my PrimaryPart

image

It has a PrimaryPart so I am confused as to why it is getting annoyed, it didn’t have this problem before I redid the code to fit what you had written - any clues?