Placement rotation system help

Hello! I am making some sort of a placement system.
I wanted to implement some rotation.
But for some reason every new object I place.
Has a new rotation or something.
I press the same button for every object btw.

here’s the module script :

local module = {}

local touching = script.Touching

function module.UpdateObject(object, mouse, plr, rotationY)
	
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {object, plr.Character}
	rayParams.FilterType = Enum.RaycastFilterType.Exclude

	local unitRay = mouse.UnitRay
	local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, rayParams)

	if raycastResult then
		
		local hitPosition = raycastResult.Position
		object.CFrame = CFrame.new(hitPosition) * CFrame.Angles(0, math.rad(rotationY), 0) * CFrame.new(0, object.Size.Y / 2, 0)
		
	end


	local collisions = workspace:GetPartsInPart(object)
	
	if #collisions > 0 then
		
		object.BrickColor = BrickColor.new("Really red")
		touching.Value = true
		
	else
		
		object.BrickColor = BrickColor.new("Dark stone grey")
		touching.Value = false
		
	end
	
end

function module.PlaceObject(object, plr)
	
	if touching.Value == true then return end

	local newObject = object:Clone()
	newObject.CFrame = object.CFrame
	newObject.Transparency = 0
	newObject.CastShadow = true
	newObject.Anchored = true
	newObject.CanCollide = true
	newObject.Parent = workspace
	
	plr.PlayerGui.Main.Main.Visible = true
	object:Destroy()
	
	
	
end

return module

and the local script.

local rs = game:GetService("ReplicatedStorage")
local plrs = game:GetService("Players")
local uis = game:GetService("UserInputService")

local objects = rs.Objects:GetChildren()

local plr = plrs.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()

local mouse = plr:GetMouse()
local module = require(rs.Module)

local touching = rs.Module.Touching
local isPlacing = false
local rotationY = 0

local object

script.Parent.Button.Activated:Connect(function()
	
	script.Parent.Main.Visible = not script.Parent.Main.Visible
	
end)

for _, obj in ipairs(objects) do
	
	local template = script.Object:Clone()

	template.Text = obj.Name
	template.Parent = script.Parent.Main

	template.Activated:Connect(function()
		
		script.Parent.Main.Visible = false

		object = obj:Clone()

		object.Transparency = 0.5
		object.CastShadow = false
		object.Parent = workspace
		object.Anchored = true
		object.CanCollide = false
		isPlacing = true

		local function Update()
			
			if isPlacing then
				
				module.UpdateObject(object, mouse, plr, rotationY)
				
			end
			
		end

		local function Place()
			
			if isPlacing then
				
				if touching.Value == true then return end
				
				module.PlaceObject(object, plr)
				
				isPlacing = false
				rotationY = 0
				object = nil
				
			end
			
		end

		local function Cancel()
			
			if isPlacing then
				
				uis.InputBegan:Connect(function(key)

					if key.KeyCode == Enum.KeyCode.Q then

						script.Parent.Main.Visible = true

						object:Destroy()
						rotationY = 0
						isPlacing = false

					end

				end)

			end
			
		end
		
		uis.InputBegan:Connect(function(input)
			if isPlacing then
				if input.KeyCode == Enum.KeyCode.R then
					rotationY = rotationY + 15
					Update()
				elseif input.KeyCode == Enum.KeyCode.T then
					rotationY = rotationY - 15
					Update()
				end
			end
		end)
		
		mouse.Move:Connect(Update)
		mouse.Button1Down:Connect(Place)
		mouse.Button2Down:Connect(Cancel)
		
	end)
	
end

Thank you!

1 Like

I think whats happening is that the script never disconnects the uis.InputBegan connection for rotating the object. So all the previously made connections also affect the rotationY variable.

I’d recommend initializing the rotationY variable within the same scope as the uis.InputBegan connection.
However a big problem I’m seeing in the code is that it’ll keep creating new connections which are never disconnected thus taking up more and more memory. You should only create these connections once:

uis.InputBegan:Connect(function(input)
	if isPlacing then
		if input.KeyCode == Enum.KeyCode.R then
			rotationY = rotationY + 15
			Update()
		elseif input.KeyCode == Enum.KeyCode.T then
			rotationY = rotationY - 15
			Update()
		end
	end
end)
		
mouse.Move:Connect(Update)
mouse.Button1Down:Connect(Place)
mouse.Button2Down:Connect(Cancel)

You seem to overall be creating a few connections, which you don’t disconnect, with the placement script.
You should only create these connections once or make sure they’re eventually disconnected.

1 Like

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