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!
2 comments
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)
not working