Function not properly returning causing values to increment more than once

Hello!

So basically, I am having a problem that is causing my value to increment more than once because it’s not properly returning and I am not sure why.

So pretty much, when you first select the object, it selects and rotates fine.
The issues happen when you select the second object and begin to rotate, it causes the value to increment twice.

Button script, it just clones each model in replicated storage, and creates an individual button for that object:

for i,v in pairs(Models:GetChildren()) do
	local Model = v:Clone()
	
	local VPF = Instance.new('ViewportFrame', script.Parent.Items)
	local Camera = Instance.new('Camera', VPF)
	Model:SetPrimaryPartCFrame(CFrame.new(0, 0, 0))
	Model.Parent = VPF
	VPF.CurrentCamera = Camera
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = CFrame.lookAt(Vector3.new(-10, 0, 0), Model.PrimaryPart.Position)
	VPF.BackgroundTransparency = 1
	
	local btn = Instance.new('TextButton', VPF)
	btn.Size = UDim2.new(1,0,1,0)
	btn.BackgroundTransparency = 1
	local itemPreview = script.Parent.ItemPreview
	btn.MouseEnter:Connect(function()
		itemPreview.ItemName.Text = Model.Name
		itemPreview.ItemDescription.Text = Model.Description.Value
		itemPreview:TweenPosition(UDim2.new(0, 0, -1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .25, true)
	end)
	
	btn.MouseButton1Click:Connect(function()
		local SelectionRS = game.ReplicatedStorage.Models.PlotModels[Model.Name]
		local Selection = SelectionRS:Clone()
		if not Selection.PrimaryPart then
			Selection.PrimaryPart = Selection:FindFirstChild('BoundingBox')
		end
		local canPlace = Functions.RequestCanPlace:InvokeServer(SelectionRS)
		Mouse.Move:Connect(function()
			PlacementModule:SnapToGrid(Mouse, Selection, false)
		end)
		
		UIS.InputBegan:Connect(function(key, isSystemReserved)
			if (key.UserInputType == Enum.UserInputType.MouseButton1) then
				local canPlace
				local s,e = pcall(function()
					canPlace = Functions.PurchaseObject:InvokeServer(Selection.PrimaryPart.CFrame)
				end)
				if not s then return end
				if canPlace == true then
					print('canPlace')
					return
				end
			elseif key.UserInputType == Enum.UserInputType.Keyboard then
				if key.KeyCode == Enum.KeyCode.R then
					PlacementModule:Rotate(Mouse, Selection)
				elseif key.KeyCode == Enum.KeyCode.Q then
                                    -- should cancel the selection
					PlacementModule:CancelPlacement(Mouse, Selection)
					Selection = nil
					return
				end
			end
		end)
	end)
	
	btn.MouseLeave:Connect(function()
		itemPreview:TweenPosition(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .25, true)
	end)
end

What’s going on;

It’s still detecting the key.KeyCode from the first button which is why it’s doubling the count. Try using CAS or context action service to unbind it, or add an if statement to see if you’re still on one button

1 Like

I knew about the double-input thing but wasn’t sure how to solve it. CAS looks like it may, thank you so much!

1 Like