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

Model in placement system not rotating?

I made a placement script but when I rotate it, then place it, it doesn't rotate the placed item. The client that is under this works fine but the Handler is the problem.

Client (under StarterGui):

local runService = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local UserInputService = game:GetService("UserInputService")

local Table = game.ReplicatedStorage:WaitForChild("Table"):Clone()
Table.Parent = workspace

local placed = false

mouse.Button1Up:Connect(function()
	game.ReplicatedStorage.PlaceTable:FireServer(placed,mouse.Hit)
	
	placed = not placed
end)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		print("Works")
		if placed then
			
		else
			Table.TableBase.Orientation += Vector3.new(0, 90, 0)
			game.ReplicatedStorage.Table.TableBase.Orientation += Vector3.new(0, 90, 0)
		end
	end
end)

runService.RenderStepped:Connect(function()
	if placed then
		Table.Parent = nil
	else
		if Table then
			Table:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.Position))
			Table.Parent = workspace
		else
			Table = game.ReplicatedStorage:WaitForChild("Table"):Clone()
		end
	end
end)

Handler (under ServerScriptService):

game.ReplicatedStorage.PlaceTable.OnServerEvent:Connect(function(player,placed,baseCFrame)
	if placed == true then
		if workspace:FindFirstChild(player.Name.."Table") then
			workspace:FindFirstChild(player.Name.."Table"):Destroy()
		end
	else
		local Table = game.ReplicatedStorage.Table:Clone()
		Table.Name = player.Name.."Table"
		Table:SetPrimaryPartCFrame(CFrame.new(baseCFrame.Position))
		--rotating script (I dont know what to do)
		Table.Table.Transparency = 0
		Table.Table.Anchored = false
		Table.TableBase.Anchored = false
		Table:FindFirstChild("Table").CanCollide = true
		Table.Parent = workspace
	end
end)

Thanks to anyone who can help.

1 answer

You cannot edit a part's Orientation. Instead, you can edit the CFrame property. To change the orientation of a CFrame, you can use either CFrame.Angles(), CFrame.fromOrientation(), or CFrame.lookAt().

CFrame.Angles() and CFrame.fromOrientation() rotate a CFrame using the x, y, z parameters in radians.

CFrame.lookAt() makes a CFrame look at a Vector3 world position.

For this one, we will use CFrame.Angles(), since you want Table.TableBase to have the same rotation of Table.Table. How do I get the rotation of Table.Table? It's simple! All you gotta do is get its CFrame either Table.Table.CFrame or Table.Table:GetPivot(), then call CFrame:ToEulerAnglesXYZ().

Table.Table.CFrame:ToEulerAnglesXYZ() -- does the same thing
Table.Table:GetPivot():ToEulerAnglesXYZ() -- does the same thing (only if pivot is in the origin)

Now we can use it in CFrame.Angles().

CFrame.Angles(Table.Table.CFrame:ToEulerAnglesXYZ())

To rotate Table.TableBase simply multiply its CFrame.

Table.TableBase.CFrame *= CFrame.Angles(Table.Table.CFrame:ToEulerAnglesXYZ())

BUT WAIT! If you try this, it won't rotate to the correct orientation at all! It's because the CFrame has already an orientation. In order for it to rotate at correct orientation, we must reset its orientation to 0, 0, 0 by creating a new CFrame using only the position.

CFrame.new(Table.TableBase.Position)

Now it should look like this:

Table.TableBase.CFrame = CFrame.new(Table.TableBase.Position) * CFrame.Angles(Table.Table.CFrame:ToEulerAnglesXYZ())

Alternatively, you can also use CFrame.fromOrientation() and CFrame:ToOrientation() which will achieve the same thing.

Table.TableBase.CFrame = CFrame.new(Table.TableBase.Position) * CFrame.fromOrientation(Table.Table.CFrame:ToOrientation())

The final script should look like this: (Handler under ServerScriptService)

game.ReplicatedStorage.PlaceTable.OnServerEvent:Connect(function(player,placed,baseCFrame)
	if placed == true then
		if workspace:FindFirstChild(player.Name.."'s Table") then
			workspace:FindFirstChild(player.Name.."'s Table"):Destroy()
		end
	else
		local Table = game.ReplicatedStorage.Table:Clone()
		Table.Name = player.Name.."'s Table"
		Table:SetPrimaryPartCFrame(CFrame.new(baseCFrame.Position))
		--rotating script (I dont know what to do)
		Table.TableBase.CFrame = CFrame.new(Table.TableBase.Position) * CFrame.Angles(Table.Table.CFrame:ToEulerAnglesXYZ())
		Table.Table.Transparency = 0
		Table.Table.Anchored = false
		Table.TableBase.Anchored = false
		Table:FindFirstChild("Table").CanCollide = true
		Table.Parent = workspace
	end
end)

2 comments

0

I'm trying to get the Orientation of the Table that is in Replicated storage and put that orientation into the TableBase that's now in the Workspace

0

place line 11 after line 16

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