I am trying to make a placement system and I added an event for player pressing R key for rotation of the model but it doesn’t work. I tried writing the same code into command bar and it worked. Do you have any idea why it is not working?
--Services
local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
--RemoteEvents
local RemoteEvents = RS:WaitForChild("RemoteEvents")
local placeFurniture = RemoteEvents.placeFurniture
--Furnitures
local Furnitures = RS:WaitForChild("Furnitures")
--Player Informations
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local ifPlaced = false
local ifPlacing = false
local ifRotated = false
local furniture
local function onTrigger(Furniture)
furniture = Furnitures:FindFirstChild(Furniture):Clone()
for i, v in pairs(furniture:GetChildren()) do
if v:IsA("Part") then
v.CanCollide = false
v.Transparency = 0.5
v.BrickColor = BrickColor.new("Bright green")
end
end
furniture.Parent = game.Workspace
Mouse.TargetFilter = furniture
ifPlacing = true
local loop
loop = RunService.RenderStepped:Connect(function()
local mousePos = Mouse.Hit.Position
local newCFrame = CFrame.new(mousePos)
furniture:SetPrimaryPartCFrame(newCFrame)
if ifPlaced == true then
loop:Disconnect()
ifPlaced = false
ifPlacing = false
ifRotated = false
placeFurniture:FireServer("Place", furniture.Name, furniture:GetPrimaryPartCFrame())
furniture:Destroy()
end
end)
end
placeFurniture.OnClientEvent:Connect(onTrigger)
UIS.InputBegan:Connect(function(key, gameProccesedEvent)
if key.UserInputType == Enum.UserInputType.MouseButton1 and gameProccesedEvent == false and ifPlacing == true then
ifPlaced = true
end
end)
--This is the event control the rotation
UIS.InputBegan:Connect(function(key, gameProcessedEvent)
if key.KeyCode == Enum.KeyCode.R and gameProcessedEvent == false and ifPlacing == true then
print("pasdjpSA")
if ifRotated == false then
furniture:SetPrimaryPartCFrame(furniture:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(90), 0))
ifRotated = true
else
local cframe = furniture:GetPrimaryPartCFrame()
furniture:SetPrimaryPartCFrame(furniture:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(-90), 0))
ifRotated = false
end
end
end)
This code basically clones the furniture and puts it into workspace for player to preview the placement. All the parts in the model aren’t anchored if that affects anything