Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Part Rotates After Placing?

Asked by 7 years ago

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

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago

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)
0
Thanks for the help! I have it working now!! austin10111213 28 — 7y
Ad

Answer this question