Hello! I made a building tool but I don’t know how to make it look at the camera? Currently its’ orientation is kind-of unpredictable but it mostly stays to the default one.
Could somebody please help me?
Here’s my client-side code:
-- References
local LocalPlayer = game:GetService("Players").LocalPlayer
local BuildGui = LocalPlayer:WaitForChild("PlayerGui", 60):WaitForChild("BuildGui", 60)
local Hammer = script.Parent
local mouse = LocalPlayer:GetMouse()
local CurrentCamera = game.Workspace.CurrentCamera
-- Remotes & Bindables
local BlockSelectionne = game.ReplicatedStorage.BlockSelectionne
local ConstruireBlock = game.ReplicatedStorage.ConstruireBlock
-- Variable de connection pour l'Event BlockSelectionne
local blockSelectionneConnection
local mouseButton1DownConnection
-- Faire apparaître le GUI quand le joueur équipe le marteau
Hammer.Equipped:Connect(function()
BuildGui.Enabled = true
-- Connecter l'Event BlockSelectionne
blockSelectionneConnection = BlockSelectionne.Event:Connect(function(blockName)
-- Si un bloc avait été séléctionné auparavant, on le déconnecte
if mouseButton1DownConnection then
mouseButton1DownConnection:Disconnect()
end
mouseButton1DownConnection = mouse.Button1Down:Connect(function()
print("envoyé", blockName, "à être placé")
ConstruireBlock:FireServer(tostring(blockName), CurrentCamera.CFrame, mouse.UnitRay, mouse.Target)
end)
end)
if blockSelectionneConnection then
print("connecté BlockSelectionneConnection")
end
if mouseButton1DownConnection then
print("connecté mouseButton1DownConnection")
end
end)
-- Faire disparaître le GUI quand le joueur déséquipe le marteau
Hammer.Unequipped:Connect(function()
BuildGui.Enabled = false
-- Disconnect the BlockSelectionne and mouseButton1DownConnection event when the tool is unequipped
if blockSelectionneConnection then
blockSelectionneConnection:Disconnect()
print("déconnecté BlockSelectionneConnection")
end
if mouseButton1DownConnection then
mouseButton1DownConnection:Disconnect()
print("déconnecté mouseButton1DownConnection")
end
end)
And my server-side code:
-- Remotes
local ConstruireBlock = game.ReplicatedStorage.ConstruireBlock
-- Constants
local GRIDSIZE = 2.75
local GRIDROTATION = 90
local REACH = 100
ConstruireBlock.OnServerEvent:Connect(function(player, blockName, CurrentCameraCFrame, mouseUnitRay, mouseTarget)
print(player)
local mouseRaycast = workspace:Raycast(mouseUnitRay.Origin, mouseUnitRay.Direction * REACH, RaycastParams.new{FilterDescendantsInstances={player}})
local newBlock = game.ServerStorage.Blocks[blockName]:Clone()
print(mouseRaycast)
newBlock.CFrame = CFrame.new(
math.round((mouseRaycast.Position.X + mouseRaycast.Normal.X) / GRIDSIZE) * GRIDSIZE,
math.round((mouseRaycast.Position.Y + mouseRaycast.Normal.Y) / GRIDSIZE) * GRIDSIZE,
math.round((mouseRaycast.Position.Z + mouseRaycast.Normal.Z) / GRIDSIZE) * GRIDSIZE,
)
newBlock.Parent = game.Workspace
print("a placé", newBlock.Name, "dans le serveur")
end)