I am currently making a script for when you click a button, you get to place a certain part down. Everything is working fine except for one major issue I noticed with the code. The issue is that when I place the part down, if I click again on nothing (it cannot be a button or anything else or it wont happen), a clone of that part will also spawn. This is strange because the heartbeat is made to disconnect and I can verify it is not disconnecting, ever, because of a print statement I put.
The code:
(this is gonna be long, sorry)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runservice = game:GetService("RunService")
local revent = script.Parent.PlaceTurret
local frame = script.Parent
local confirmplaces = script.Parent.ConfirmPlaces
local placeable = false
local cancel = false
for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA("TextButton") and v.Name ~= "Close" then
v.MouseButton1Click:Connect(function(c)
local tchild = v:GetChildren()
local t = tchild[1]:Clone()
t.Parent = game.Workspace
mouse.TargetFilter = t
local y = t.Size.Y
local yOffset = y/2
cancel = false
placeable = true
local heartbeat
--tween placing menu to the side and disable all the buttons so you Can Avoid Glitches!
script.Parent.Position = UDim2.new(-0.3,0,0.453,0)
--disable all turret placement buy buttons
for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA("TextButton") and v.Name ~= "Close" then
v.Active = false
end
end
--bring up cancel button
script.Parent.Parent.Cancel.Position = UDim2.new(0,0,0.9,0)
script.Parent.Parent.Cancel.Active = true
heartbeat = runservice.Heartbeat:Connect(function()
--move to mouse position
t.Position = mouse.Hit.Position + Vector3.new(0,yOffset,0)
--make ray to check if on Viable Ground!
local castRay = Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 1000)
local ignoreList = {t, player.Character}
local raycast, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
--make sure on "ground"
if raycast then
local hit = raycast
local placesleft = confirmplaces:InvokeServer()
if hit.Name == "ground" and placesleft > 0 then
t.BrickColor = BrickColor.new("Bright green")
placeable = true
else
placeable = false
t.BrickColor = BrickColor.new("Crimson")
end
end
--end if cancelled
frame.Parent.Cancel.MouseButton1Click:Connect(function()
cancel = true
--move everything back
frame.Parent.Cancel.Position = UDim2.new(-0.3,0.0,9,0)
frame.Parent.Cancel.Active = false
frame.Position = UDim2.new(0,0,0.453,0)
t:Destroy()
heartbeat:Disconnect()
end)
--place part
mouse.Button1Down:connect(function()
print("Click")
if placeable == true and cancel == false then
placeable = false
--place and stuff
revent:FireServer(tchild[1].Name, t.CFrame, frame, placeable)
t:Destroy()
--move back cancelbutton
frame.Parent.Cancel.Position = UDim2.new(-0.3,0,0.9,0)
frame.Parent.Cancel.Active = false
--move building menu back to position
script.Parent.Position = UDim2.new(0,0,0.453,0)
for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA("TextButton") and v.Name ~= "Close" then
v.Active = true
end
end
heartbeat:Disconnect()
end
end)
end)
end)
end
end
Can putting a runservice inside of a button mousebutton1click event cause this issue?