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

Part Rotates After Placing?

Hi, I'm making a build preview and everything works fine now except the part continues to rotate after being placed if you keep pressing "r". How do I disconnect the function which makes it rotate?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local blankPart = game.ServerStorage:WaitForChild('WoodWall')


script.Parent.MouseButton1Down:connect(function()
	if player.leaderstats.Wood.Value >= 5 then
		player.leaderstats.Wood.Value = player.leaderstats.Wood.Value - 5
		script.Parent.Parent.Parent.Parent.Visible = false
	    local part = blankPart:Clone() 
     	mouse.TargetFilter = part
        moveConn = mouse.Move:connect(function()
        	local p = mouse.Hit.p
       	    part.Position = Vector3.new(math.floor(p.X),math.ceil(p.Y),math.floor(p.Z)) 
            mouse.KeyDown:connect(function(key)
		    	if (key == "r") then
					part.CFrame = part.CFrame*CFrame.Angles(0,math.pi/2,0)
				end
			end)
 end)



    mouse.Button1Down:wait() 
    moveConn:disconnect() 
	mouse.KeyDown:disconnect(key)

	
end
end)

1 answer

✓ Accepted answer

You have the right idea, but you're not using the disconnect function correctly. You need to define a variable for the event connection. This way, you can use the disconnect function on that connection variable.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local blankPart = game.ServerStorage:WaitForChild('WoodWall')

script.Parent.MouseButton1Down:connect(function()
	if player.leaderstats.Wood.Value >= 5 then
		player.leaderstats.Wood.Value = player.leaderstats.Wood.Value - 5
		script.Parent.Parent.Parent.Parent.Visible = false
		local part = blankPart:Clone() 
		mouse.TargetFilter = part
		local moveConn = mouse.Move:connect(function()
			local p = mouse.Hit.p
			part.Position = Vector3.new(math.floor(p.X),math.ceil(p.Y),math.floor(p.Z))
			--Assign event a connection variable
			keys = mouse.KeyDown:connect(function(key)
				if (key == "r") then
					part.CFrame = part.CFrame*CFrame.Angles(0,math.pi/2,0)
				end
			end)
		end)
		mouse.Button1Down:wait() 
		moveConn:disconnect() 
		keys:disconnect(); --disconnect from connection variable
	end
end)

1 comment

0

Thanks for the help! I have it working now!!

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