Help with script not working! Having trouble setting the location and rotation of a model!

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = game.Workspace:WaitForChild("House1")
local Position = part:GetPivot()
local tweenService = game:GetService("TweenService")


local CFrameValue = Instance.new("CFrameValue")
CFrameValue.Parent = game.Workspace



local UIS = game:GetService("UserInputService")

local EV = game.ReplicatedStorage.Placing

local Xup = nil
local Zup = nil

local y = Position.Y

local OriX, OriZ, OriY = part:GetPivot():ToOrientation()
local canPlace = false

local function Move()
	wait(0.25)
	CFrameValue.Value = part:GetPrimaryPartCFrame()
	
	
	
	local x,z = mouse.Hit.X, mouse.Hit.Z
	
	Xup = math.floor(x / 4 + 0.5) * 4
	Zup = math.floor(z / 4 + 0.5) * 4

	local tweenInfo = TweenInfo.new(
		0.25,
		Enum.EasingStyle.Back,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local tweenPart = tweenService:Create(CFrameValue, tweenInfo, {Value = CFrame.new(Xup,y,Zup)})
	
	tweenPart:Play()
	
	
	
	local filter = OverlapParams.new()
	filter.FilterType = Enum.RaycastFilterType.Exclude
	filter.FilterDescendantsInstances = {game.Workspace.Baseplate, player.Character, part}

	local foundParts = workspace:GetPartsInPart(part.HitBox, filter)
	
	if foundParts[1] ~= nil then
		part.Highlight.FillColor = Color3.new(255,0,0)
		canPlace = false
	else

		part.Highlight.FillColor = Color3.new(0,255,0)
		canPlace = true
	end
	
end

local function Rotate(input)
	if input.KeyCode == Enum.KeyCode.R then

		local rotation = CFrame.Angles(0, math.rad(90), 0)
		local modelCFrame = part:GetPivot()
		part:PivotTo(modelCFrame * rotation)
	end	
end

mouse.Button1Down:Connect(function()
	if canPlace == true then
		local Pos = Vector3.new(Xup,y,Zup)
		
		local Ori = Vector3.new(OriX,OriY,OriZ)
		print(OriX,OriY,OriZ)
		EV:FireServer(Pos, Ori)
	else
		print("Cannot place here")
	end

end)


CFrameValue:GetPropertyChangedSignal("Value"):connect(function()
	part:SetPrimaryPartCFrame(CFrameValue.Value)
end)
	
mouse.Move:Connect(Move)
UIS.InputBegan:Connect(Rotate)

This is the code I am trying to make work but for some reason it does not work. (Sorry in advance if the variables have funny names)

I have gotten the tween to work and the rotate sort of works. The problem with the rotate is that you press R and it rotates but as soon as you move your mouse the rotate cancels and goes back to it’s original rotation. I know I am doing something wrong but I just don’t know what. If someone could also explain if their is an easier way to rotate a model than :pivotTo or explain more in depth about it that would be greatly appreciated!

Thanks, Hellobeede

1 Like

Oh! What I am trying to accomplish is a grid placement system that can place a house and rotate as well. Sorry for forgetting to mention in the post! I know there are posts explaining this but I just need it dumbed down for me.
Thanks

local Ev = game.ReplicatedStorage.Placing

local part = game.Workspace.House1

Ev.OnServerEvent:Connect(function(player, pos, ori)
local ClonePart = part:Clone()
ClonePart.Parent = workspace
ClonePart:MoveTo(pos)
ClonePart:PivotTo(ori)
ClonePart.Highlight:Destroy()
end)

I don’t even know where to start here…

Hello so the issue is that your not changing the variables of the rotation variables to the new rotation whenever you press r to rotate. So when you move your mouse it returns the rotation to default. Another thing is the function on move() doesn’t add the rotation I believe only the position so maybe this script will work instead

local tweenPart = tweenService:Create(CFrameValue, tweenInfo, {Value = CFrame.new(Xup,y,Zup) * CFrame.Angles(math.rad(OriX), math.rad(OriY), math.rad(OriZ))})

I added this so it applies the current rotation whenever you move the mouse. Because before it only applied the current position and not the rotation too

OriX, OriY, OriZ = part.Orientation.X, part.Orientation.Y, part.Orientation.Z

I added this on Rotate() So it saves the new rotation.

Here is the new script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = game.Workspace:WaitForChild("House1")
local Position = part:GetPivot()
local tweenService = game:GetService("TweenService")


local CFrameValue = Instance.new("CFrameValue")
CFrameValue.Parent = game.Workspace



local UIS = game:GetService("UserInputService")

local EV = game.ReplicatedStorage.Placing

local Xup = nil
local Zup = nil

local y = Position.Y

local OriX, OriZ, OriY = part:GetPivot():ToOrientation()
local canPlace = false
local isRotating = false
local function Move()
	if not isRotating then
		wait(0.25)
		CFrameValue.Value = part:GetPrimaryPartCFrame()

		local x,z = mouse.Hit.X, mouse.Hit.Z
		Xup = math.floor(x / 4 + 0.5) * 4
		Zup = math.floor(z / 4 + 0.5) * 4

		local tweenInfo = TweenInfo.new(
			0.25,
			Enum.EasingStyle.Back,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local tweenPart = tweenService:Create(CFrameValue, tweenInfo, {Value = CFrame.new(Xup,y,Zup) * CFrame.Angles(math.rad(OriX), math.rad(OriY), math.rad(OriZ))})
		tweenPart:Play()

		local filter = OverlapParams.new()
		filter.FilterType = Enum.RaycastFilterType.Exclude
		filter.FilterDescendantsInstances = {game.Workspace.Baseplate, player.Character, part}

		local foundParts = workspace:GetPartsInPart(part.HitBox, filter)

		if foundParts[1] ~= nil then
			part.Highlight.FillColor = Color3.new(255,0,0)
			canPlace = false
		else

			part.Highlight.FillColor = Color3.new(0,255,0)
			canPlace = true
		end
	end
end

local function Rotate(input)
	if input.KeyCode == Enum.KeyCode.R then
		local rotation = CFrame.Angles(0, math.rad(90), 0)
		local modelCFrame = part:GetPivot()
		part:PivotTo(modelCFrame * rotation)
		OriX, OriY, OriZ = part.Orientation.X, part.Orientation.Y, part.Orientation.Z
	end	
end

mouse.Button1Down:Connect(function()
	if canPlace == true then
		local Pos = Vector3.new(Xup,y,Zup)

		local Ori = Vector3.new(OriX,OriY,OriZ)
		print(OriX,OriY,OriZ)
		EV:FireServer(Pos, Ori)
	else
		print("Cannot place here")
	end

end)


CFrameValue:GetPropertyChangedSignal("Value"):connect(function()
	part:SetPrimaryPartCFrame(CFrameValue.Value)
end)

mouse.Move:Connect(Move)
UIS.InputBegan:Connect(Rotate)

If you still have any issues please reply and tell me, and I will assist you

It worked! Thanks so much I was confused for a while but this helped! Thanks!!!

1 Like

I am glad to hear it worked:) If any issues still arises feel free to reply and I will help you.

1 Like

Wait what about the Isrotating

Oh my bad, just remove that. The reason I added that was I thought maybe it was interfering with the rotation when you moved.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = game.Workspace:WaitForChild("House1")
local Position = part:GetPivot()
local tweenService = game:GetService("TweenService")


local CFrameValue = Instance.new("CFrameValue")
CFrameValue.Parent = game.Workspace



local UIS = game:GetService("UserInputService")

local EV = game.ReplicatedStorage.Placing

local Xup = nil
local Zup = nil

local y = Position.Y

local OriX, OriZ, OriY = part:GetPivot():ToOrientation()
local canPlace = false
local function Move()
	wait(0.25)
	CFrameValue.Value = part:GetPrimaryPartCFrame()

	local x,z = mouse.Hit.X, mouse.Hit.Z
	Xup = math.floor(x / 4 + 0.5) * 4
	Zup = math.floor(z / 4 + 0.5) * 4

	local tweenInfo = TweenInfo.new(
		0.25,
		Enum.EasingStyle.Back,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local tweenPart = tweenService:Create(CFrameValue, tweenInfo, {Value = CFrame.new(Xup,y,Zup) * CFrame.Angles(math.rad(OriX), math.rad(OriY), math.rad(OriZ))})
	tweenPart:Play()

	local filter = OverlapParams.new()
	filter.FilterType = Enum.RaycastFilterType.Exclude
	filter.FilterDescendantsInstances = {game.Workspace.Baseplate, player.Character, part}

	local foundParts = workspace:GetPartsInPart(part.HitBox, filter)

	if foundParts[1] ~= nil then
		part.Highlight.FillColor = Color3.new(255,0,0)
		canPlace = false
	else

		part.Highlight.FillColor = Color3.new(0,255,0)
		canPlace = true
	end
end

local function Rotate(input)
	if input.KeyCode == Enum.KeyCode.R then
		local rotation = CFrame.Angles(0, math.rad(90), 0)
		local modelCFrame = part:GetPivot()
		part:PivotTo(modelCFrame * rotation)
		OriX, OriY, OriZ = part.Orientation.X, part.Orientation.Y, part.Orientation.Z
	end	
end

mouse.Button1Down:Connect(function()
	if canPlace == true then
		local Pos = Vector3.new(Xup,y,Zup)

		local Ori = Vector3.new(OriX,OriY,OriZ)
		print(OriX,OriY,OriZ)
		EV:FireServer(Pos, Ori)
	else
		print("Cannot place here")
	end

end)


CFrameValue:GetPropertyChangedSignal("Value"):connect(function()
	part:SetPrimaryPartCFrame(CFrameValue.Value)
end)

mouse.Move:Connect(Move)
UIS.InputBegan:Connect(Rotate)

Ok Thanks! This really helped me!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.