Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

It's only rotating? Not following the mouses position? (cmon, it only take a few minutes to answer)

So, i made a viewportframe, in the viewportframe i inserted a note. You can hold your mouse down and move it, to rotate the note. My problem is that it's just spinning, not following the mouse's position?

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local pressed = false
local moving = false

Mouse.Button1Down:Connect(function()
	
	pressed = true
end)

Mouse.Move:Connect(function()
	moving = true
	
end)

Mouse.Button1Up:Connect(function()
	
	moving = false
	pressed = false
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if moving and pressed then
		if script.Parent.Visible == true then
		script.Parent:FindFirstChildOfClass("Model").PrimaryPart.CFrame =  script.Parent:FindFirstChildOfClass("Model").PrimaryPart.CFrame * CFrame.Angles(math.rad(Mouse.X/500), 0, 0) 
	end
	end
end)

2 comments

0

Why not just do for line 26: script.Parent:FindFirstChildOfClass("Model").PrimaryPart.CFrame = CFrame.new( script.Parent:FindFirstChildOfClass("Model").PrimaryPart.Position) * CFrame.Angles(math.rad(Mouse.X/500), 0, 0)

0

not working

1 answer

What you are looking for is adding a vector to the primary part. You will need to experiment with different values, as this will depend on part size, viewportframe's camera positition, etc...

script.Parent:FindFirstChildOfClass("Model").PrimaryPart.CFrame = (  script.Parent:FindFirstChildOfClass("Model").PrimaryPart.CFrame  + Vector3.new(Mouse.X/10, Mouse.Y/10,0) ) * CFrame.Angles(math.rad(Mouse.X/500), 0, 0) 

I suggest to optimize your code a little bit and find this part only once:


local part = script.Parent:FindFirstChildOfClass("Model").PrimaryPart

game:GetService("RunService").RenderStepped:Connect(function()
    if moving and pressed then
        if script.Parent.Visible == true then
       part.CFrame =  (part.CFrame + Vector3.new(Mouse.X/10, Mouse.Y/10,0) ) * CFrame.Angles(math.rad(Mouse.X/500), 0, 0) 
    end
    end
end)

To reduce unnecessary execution, connect the function only when needed.

local part = script.Parent:FindFirstChildOfClass("Model").PrimaryPart

local function Step()

	if script.Parent.Visible == true then
		part.CFrame =  (part.CFrame + Vector3.new(Mouse.X/10, Mouse.Y/10,0) ) * CFrame.Angles(math.rad(Mouse.X/500), 0, 0) 
	end

end

local connection

Mouse.Button1Down:Connect(function()

	pressed = true

end)

Mouse.Move:Connect(function()
    moving = true
	if pressed and not connection then
		connection = game:GetService("RunService").RenderStepped:Connect(Step)
	end
end)

Mouse.Button1Up:Connect(function()

    moving = false
    pressed = false
	if connection then
		connection:Disconnect()
		connection = nil
	end
end)

Finally, unless you are moving camera around, it is recommended to use Heartbeat instead of RenderStepped

connection = game:GetService("RunService").Heartbeat:Connect(Step)

Have a nice scripting session!

1 comment

0

It's still spinning around, and it's going backwards so it goes further and further away. Not working, maybe you were right about organizing but quite frankly this functions worse than the script i made. If we're speaking of terms of work

This discussion is preserved as it was. New answers, comments, and votes are closed.