Hey everyone, Dose anyone know why you dont have to hold the mouse button down to rotate the object, because right now if you move your mouse the item just rotates we want it so if you hold down the mousebutton1 and move you mouse it will rotate with it! Thank you.
local uiInput = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local Tip = script.Parent:WaitForChild("Tip")
local Tip2 = script.Parent:WaitForChild("Tip2")
local Tip3 = script.Parent:WaitForChild("Tip3")
local Tip4 = script.Parent:WaitForChild("Tip4")
local toolsFolder = replicatedStorage:WaitForChild("Tools")
local remoteEvent = replicatedStorage:WaitForChild("GiveToolEvent")
local ChangeMouseEvent = replicatedStorage:WaitForChild("ChangeMouseEvent")
local ResetMouseEvent = replicatedStorage:WaitForChild("ResetMouseEvent")
local viewportFrame = script.Parent.ViewportFrame
local VPFcam = Instance.new("Camera")
VPFcam.Parent = viewportFrame
viewportFrame.CurrentCamera = VPFcam
VPFcam.CFrame = CFrame.new(0, 0, 15)
VPFcam.FieldOfView = 20
local currentModel
local selectedItemName
local interactionEnabled = true
local mouseInDisplay, holdInDisplay = false, false
local currentX, currentY
local zoomStep = 1
local rotationStep = math.rad(10)
local function clearViewport()
for _, obj in ipairs(viewportFrame:GetChildren()) do
if obj:IsA("Model") or obj:IsA("BasePart") then
obj:Destroy()
end
end
end
local function changeMouseCursor()
local newCursorId = "http://www.roblox.com/asset/?id=0"
ChangeMouseEvent:FireServer(newCursorId)
end
local function resetMouse()
ResetMouseEvent:FireServer()
end
local function displayInViewport(part)
if not interactionEnabled then return end
clearViewport()
Tip.Visible = true
Tip2.Visible = true
Tip3.Visible = true
Tip4.Visible = true
changeMouseCursor()
viewportFrame.Visible = true
local clonedPart = part:Clone()
currentModel = clonedPart
selectedItemName = part.Name
clonedPart.Parent = viewportFrame
if clonedPart:IsA("Model") and clonedPart.PrimaryPart then
clonedPart:SetPrimaryPartCFrame(CFrame.new(0, 0, 0))
elseif clonedPart:IsA("BasePart") then
clonedPart.CFrame = CFrame.new(0, 0, 0)
end
end
local function giveToolToPlayer(selectedToolName)
remoteEvent:FireServer(selectedToolName)
clearViewport()
Tip.Visible = false
Tip2.Visible = false
Tip3.Visible = false
Tip4.Visible = false
resetMouse()
viewportFrame.Visible = false
interactionEnabled = false
end
for _, part in ipairs(workspace:GetDescendants()) do
if part:IsA("ClickDetector") then
part.MouseClick:Connect(function(player)
local parent = part.Parent
if parent:IsA("BasePart") or parent:IsA("Model") then
displayInViewport(parent)
end
end)
end
end
workspace.DescendantAdded:Connect(function(descendant)
if descendant:IsA("ClickDetector") then
descendant.MouseClick:Connect(function(player)
local parent = descendant.Parent
if parent:IsA("BasePart") or parent:IsA("Model") then
displayInViewport(parent)
end
end)
end
end)
viewportFrame.MouseEnter:Connect(function()
mouseInDisplay = true
end)
viewportFrame.MouseLeave:Connect(function()
mouseInDisplay = false
end)
uiInput.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and mouseInDisplay then
holdInDisplay = true
currentX, currentY = nil, nil
end
if input.KeyCode == Enum.KeyCode.X and selectedItemName and interactionEnabled then
giveToolToPlayer(selectedItemName)
end
end)
uiInput.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C and interactionEnabled then
clearViewport()
Tip.Visible = false
Tip2.Visible = false
Tip3.Visible = false
Tip4.Visible = false
viewportFrame.Visible = false
currentModel = nil
selectedItemName = nil
resetMouse()
end
end)
uiInput.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseWheel and mouseInDisplay then
local deltaZoom = input.Position.Z * zoomStep
local currentDistance = (VPFcam.CFrame.Position - Vector3.new(0, 0, 0)).Magnitude
local newDistance = currentDistance + deltaZoom
if newDistance < 10 then
newDistance = 10
elseif newDistance > 20 then
newDistance = 20
end
local direction = (VPFcam.CFrame.Position - Vector3.new(0, 0, 0)).Unit
VPFcam.CFrame = CFrame.new(direction * newDistance, Vector3.new(0, 0, 0))
end
end)
uiInput.InputBegan:Connect(function(input)
if currentModel then
if input.KeyCode == Enum.KeyCode.E then
local rotation = CFrame.Angles(0, rotationStep, 0)
if currentModel:IsA("Model") and currentModel.PrimaryPart then
currentModel:SetPrimaryPartCFrame(currentModel.PrimaryPart.CFrame * rotation)
elseif currentModel:IsA("BasePart") then
currentModel.CFrame = currentModel.CFrame * rotation
end
elseif input.KeyCode == Enum.KeyCode.Q then
local rotation = CFrame.Angles(0, -rotationStep, 0)
if currentModel:IsA("Model") and currentModel.PrimaryPart then
currentModel:SetPrimaryPartCFrame(currentModel.PrimaryPart.CFrame * rotation)
elseif currentModel:IsA("BasePart") then
currentModel.CFrame = currentModel.CFrame * rotation
end
end
end
end)
viewportFrame.MouseMoved:Connect(function(x, y)
if not holdInDisplay or not currentModel then return end
if currentX and currentY then
local deltaX = (x - currentX) * 0.02
local deltaY = (y - currentY) * 0.02
local rotation = CFrame.Angles(deltaY, -deltaX, 0)
if currentModel:IsA("Model") and currentModel.PrimaryPart then
currentModel:SetPrimaryPartCFrame(currentModel.PrimaryPart.CFrame * rotation)
elseif currentModel:IsA("BasePart") then
currentModel.CFrame = currentModel.CFrame * rotation
end
end
currentX, currentY = x, y
end)