I’m creating a placing system but everytime i click the place button then the object flies in the sky
local script
--Replicated Storage
local RepStorage = game:GetService("ReplicatedStorage")
local PlacementEvent = RepStorage.PlacementEvent
--folders
local ComputerFolder = RepStorage:WaitForChild('ComputerFolder')
local CoderFolder = RepStorage:WaitForChild('ProgrammerFolder')
--Player Refrences
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Mouse = player:GetMouse()
--Gui ELements
local PlayerInv = script.Parent:WaitForChild('PlayerInvFrame')
local ScrollingFrame = PlayerInv:WaitForChild('ScrollingFrame')
local ObjectFrame = ScrollingFrame:WaitForChild('ObjectFrame')
local PlayerInvOutline = script.Parent:WaitForChild("PlayerInvFrameShade")
--User Input Service and Run Service
local UIS = game:GetService('UserInputService')
local RunService = game:GetService('RunService')
--Booleans
local PlacingObject = false
local RotatingObject = false
--Loops through childern in object frame
for i, Button in pairs(ObjectFrame:GetChildren()) do
--Checks whether the child is a textbutton
if Button:IsA('TextButton') then
Button.MouseButton1Click:Connect(function()
--Checks if the boolean is set to false
if PlacingObject == false then
--Sets Bool to true
PlacingObject = true
--Sets Frame Visibility to false
PlayerInv.Visible = false
PlayerInvOutline.Visible = false
--Sets Rotation Amounts to 0
local RotationAmounts = 0
--Finds the first child in the computer folder and setting it to button name
local PreiviewObject = ComputerFolder:FindFirstChild(Button.Name):Clone()
--puts the object in the workspace
PreiviewObject.Parent = game.Workspace
--Loops through the descendants of the object
for i,Parts in pairs(PreiviewObject:GetDescendants()) do
-- checks whethere the object is a part
if Parts:IsA("BasePart") then
--Sets the parts to
Parts.CanCollide = false
end
end
--if the input started the it will start a function
UIS.InputBegan:Connect(function(Key, GameProccesed)
--if its not proccesed
if not GameProccesed then
--if the player clicks r then the boolean will become true
if Key.KeyCode == Enum.KeyCode.R then
RotatingObject = true
--while the bool is true the rotation amouts +2
while RotatingObject == true do
task.wait()
RotationAmounts += 2
end
end
end
end)
--Input ended
UIS.InputEnded:Connect(function(Key)
--if key is r then bool = false
if Key.KeyCode == Enum.KeyCode.R then
RotatingObject = false
end
end)
--Runs every frame
RunService.RenderStepped:Connect(function()
-- checks if the bool is = to true if so then it will set the mouse targetfilter to the preiviewobject
if PlacingObject == true then
Mouse.TargetFilter = PreiviewObject
--checks the preiviewobject childeren to find the mainpart
if PreiviewObject:FindFirstChild("MainPart") then
local ObjectCFrame=CFrame.new(Mouse.Hit.Position.X,Mouse.Hit.Position.Y+ PreiviewObject.PrimaryPart.Size.Y/2,Mouse.Hit.Position.Z)
local ObjectAngles=CFrame.Angles(0,math.rad(RotationAmounts),0)
PreiviewObject:SetPrimaryPartCFrame(ObjectCFrame * ObjectAngles)
end
end
end)
Mouse.Button1Up:Connect(function()
if PlacingObject == true then
PlacingObject =false
PlacementEvent:FireServer(PreiviewObject.Name, PreiviewObject.PrimaryPart.CFrame)
PlayerInv.Visible = true
PlayerInvOutline.Visible = true
PreiviewObject:Destroy()
end
end)
end
end)
end
end
server script
local PlacementEvent =game.ReplicatedStorage.PlacementEvent
local ObjectFolder =game.ReplicatedStorage:WaitForChild("ComputerFolder")
PlacementEvent.OnServerEvent:Connect(function(Player,PreviewObject,ObjectCFrame)
local Object = ObjectFolder:FindFirstChild(PreviewObject):Clone()
Object:SetPrimaryPartCFrame(ObjectCFrame)
Object.Parent = game.Workspace
end)
Edit: This is a video of the problem
robloxapp-20220707-0132439.wmv (1.3 MB)