So I am working on a placement system but I can't seem to get the model to rotate. I have tried different ways of CFraming but the model keeps teleporting away from me and is not rotating as far as I can tell. I want to rotate the model by 90 degrees.
Heres a snippet of my code:
local UserInputService = game:GetService("UserInputService") function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.R then Model:SetPrimaryPartCFrame(CFrame.Angles(0, math.deg(90), 0)) end end UserInputService.InputBegan:Connect(onKeyPress)
Here is all of my code:
local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local PosX local PosY local PosZ local Model local Toggle = nil local Gui = script.Parent local Button = Gui.TextButton local Button2 = Gui.Model1 local IsPlacing = nil local GridId = "rbxassetid://13786085" local UserInputService = game:GetService("UserInputService") local function SnapToGrid() local Snap = 2 local Grid = 2 local RoundVariable = 0.5 if Model:IsA("Model") then PosY = Model.PrimaryPart.Position.Y Grid = 4 Snap = 4 elseif Model:IsA("Part") then PosY = Model.Position.Y Grid = 2 Snap = 2 end PosX = math.floor(Mouse.Hit.X / Snap + RoundVariable) * Grid PosZ = math.floor(Mouse.Hit.Z / Snap + RoundVariable) * Grid end local function Placement() if Toggle == true and Model:IsA("Part") and Mouse.Target and Mouse.Target.Name == "Baseplate" then local PlacedModel = Model:Clone() PlacedModel.Parent = workspace.Clones Toggle = nil IsPlacing = nil game.Workspace.Baseplate.Texture.Texture = "" Model.CanCollide = true Model.Position = Vector3.new(PosX, PosY, PosZ) elseif Toggle == true and Model:IsA("Model") then Model.PrimaryPart.Transparency = 1 local PlacedModel = Model:Clone() PlacedModel.Parent = workspace.Clones Toggle = nil IsPlacing = nil game.Workspace.Baseplate.Texture.Texture = "" local ModelProperties = PlacedModel:GetChildren() for _, Children in pairs(ModelProperties) do Children.CanCollide = false end end end local function Movement() if IsPlacing == true and Model:IsA("Model") and Mouse.Target and Mouse.Target.Name == "Baseplate" then Mouse.TargetFilter = Model SnapToGrid() Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ)) Toggle = true local ModelTable = Model:GetChildren() for _, Children in pairs(ModelTable) do Children.CanCollide = false Children.Anchored = true end elseif IsPlacing == true and Model:IsA("Part") and Mouse.Target and Mouse.Target.Name == "Baseplate" then Mouse.TargetFilter = Model SnapToGrid() Model.Position = Vector3.new(PosX, PosY, PosZ) Toggle = true Model.CanCollide = false Model.Anchored = true end end local function StartPlacement1() if Player.leaderstats.Cash.Value >= 50 then game.Workspace.Baseplate.Texture.Texture = GridId Model = game.ReplicatedStorage.Movement:Clone() Model.Parent = workspace Mouse.Move:Connect(Movement) IsPlacing = true Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - 50 end end local function StartPlacement2() if Player.leaderstats.Cash.Value >= 100 then game.Workspace.Baseplate.Texture.Texture = GridId Model = game.ReplicatedStorage.Tree:Clone() Model.Parent = workspace Mouse.Move:Connect(Movement) IsPlacing = true Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - 100 end end function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.R and IsPlacing and Model:IsA("Model") then Model:SetPrimaryPartCFrame(CFrame.Angles(0, math.deg(90), 0)) end end UserInputService.InputBegan:Connect(onKeyPress) Button.MouseButton1Click:Connect(StartPlacement1) Button2.MouseButton1Click:Connect(StartPlacement2) Mouse.Button1Down:Connect(Placement)
I have tried rotating with math.rad as well as math.deg. Neither worked. I also tried using other CFrame types other than CFrame.Angles()
. I am not getting any errors in the output either.
Everything else in my placement system works just this does not.
Thanks to who ever helps!
You are able to record a variable that represents the rotation the model is currently up to (and add 90 to this every time the user presses R). If you do this, while placing you are able to rotate it by that many degrees with CFrame.Angles. For example:
local CurrentModel local CurrentRot = 0 Mouse.Move:Connect(function() if not CurrentModel then return end CurrentModel:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p) * CFrame.Angles(0,math.rad(CurrentRot),0) end) game:GetService("UserInputService").InputBegan:Connect(function(Key,IsTyping) if not IsTyping then if Key.KeyCode == Enum.KeyCode.R then CurrentRot = CurrentRot + 90 end end end) local function SelectModel(Model) Mouse.TargetFilter = Model CurrentModel = Model end SelectModel(workspace.TestModel)
This example would move the model to the mouse position whenever the mouse was moved, and rotate it by the current rotation it is up to.