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