Script doesnt take 1 off of leaderstat s

I have a problem with a placement system, if you place something, it takes 1 off as intended.
but if you place it again it takes 2 off. and then if you close the Placement tool and click without anything equiped it still takes the value from the leaderstats.

local placementEvent = game.ReplicatedStorage.PlacementEvent
local ObjectFolder = game.ReplicatedStorage:WaitForChild("ObjectFolder")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

local Frame = script.Parent:WaitForChild("BackroundFrame"):WaitForChild("FrontFrame"):WaitForChild("BuildingFrame")
local UserImputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local PlacingObject = false
local RotatingObject = false



for i, Button in pairs(Frame:GetChildren()) do
	if Button:IsA("TextButton") then
		Button.MouseButton1Click:Connect(function()
			local player = game.Players.LocalPlayer

			for _, Button in ipairs(Frame:GetChildren()) do
				if Button:IsA("TextButton") then
					Button.MouseButton1Click:Connect(function()
						if player.leaderstats:FindFirstChild(Button.Name) then
							if player.leaderstats[Button.Name].Value <= 0 then
								return nil
							else

								if PlacingObject == false then

									PlacingObject = true
									Frame.Parent.Parent.Visible = false

									local RotatingAmount = 1

									local PrevieuwObject = ObjectFolder:FindFirstChild(Button.Name):Clone()
									PrevieuwObject.Parent = game.Workspace

									for i, Parts in pairs(PrevieuwObject:GetDescendants()) do

										if Parts:IsA("BasePart") then

											Parts.Transparency = 0.5
											Parts.CanCollide = false						
										end
									end

									UserImputService.InputBegan:Connect(function(Key, GameProcessed)

										if not GameProcessed then

											if Key.KeyCode == Enum.KeyCode.R then

												RotatingObject = true

												while RotatingObject == true do
													wait()
													RotatingAmount += 2
												end							
											end						
										end					
									end)

									UserImputService.InputEnded:Connect(function(Key)

										if Key.KeyCode == Enum.KeyCode.R then

											RotatingObject = false
										end
									end)

									RunService.RenderStepped:Connect(function()

										if PlacingObject == true then

											mouse.TargetFilter = PrevieuwObject

											if PrevieuwObject:FindFirstChild("MainPart") then

												local ObjectCFrame = CFrame.new(mouse.Hit.Position.X, mouse.Hit.Position.Y + PrevieuwObject.PrimaryPart.Size.Y / 2, mouse.Hit.Position.Z)
												local ObjectAngles = CFrame.Angles(0, math.rad(RotatingAmount), 0)

												PrevieuwObject:SetPrimaryPartCFrame(ObjectCFrame * ObjectAngles)
											end
										end			
									end)

									mouse.Button1Up:Connect(function()
										player.leaderstats:FindFirstChild(Button.Name).Value = player.leaderstats:FindFirstChild(Button.Name).Value - 1

										if PlacingObject == true then

											PlacingObject = false

											placementEvent:FireServer(PrevieuwObject.Name, PrevieuwObject.PrimaryPart.CFrame)

											Frame.Visible = true
											PrevieuwObject:Destroy()
										end
									end)
								end
							end
				        end
					end)
				end
	
			end
		end)
   end
end
	



			

	

	
	
2 Likes

here is where i take the value off

1 Like

The problem is that you are connecting to an event every time that button is activated.

If you remove the following lines of code, as well as their associated end statements, the code should work as intended:


Also, you should change leaderstat things from server scripts, not from client scripts

3 Likes

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