This is my script for making a building system and when I place the first block it works normally but when I create a second it duplicates…
I thought the debounce would work, but it didn’t. I created a second debounce and it still didn’t work!
These two are the only scripts, one locally and one server sided
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buildings = ReplicatedStorage:WaitForChild("Buildings")
local remotes = ReplicatedStorage:WaitForChild("Remotes")
local placeBlockEvent = remotes:WaitForChild("placeBlockEvent")
local tempBlocks = workspace.tempBlocks
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local length = 100
local frame = script.Parent.Frame
local placingBlock = false
local debounce = false
local function placeBlockfunc(blockType)
frame.Visible = false
placingBlock = true
local goodToPlace = false
local block = buildings:FindFirstChild(blockType):Clone()
block.Parent = tempBlocks
game:GetService("RunService").RenderStepped:Connect(function()
local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character, block}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * length, raycastParams)
if raycastResult then
block.Position = raycastResult.Position
goodToPlace = true
end
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if placingBlock == true then
if debounce == false then
debounce = true
if goodToPlace == true then
local position1 = block.position
placingBlock = placeBlockEvent:InvokeServer(blockType, position1)
if placingBlock == true then
placingBlock = false
frame.Visible = true
debounce = false
block:Destroy()
end
end
end
end
end
end)
end)
end
script.Parent.Frame.BigBlockButton.MouseButton1Click:Connect(function()
local blockType = "BigBlock"
placeBlockfunc(blockType)
end)
script.Parent.Frame.SmallBlockButton.MouseButton1Click:Connect(function()
local blockType = "SmallBlock"
placeBlockfunc(blockType)
end)
This is the code for the server script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local buildings = ReplicatedStorage:WaitForChild("Buildings")
local remotes = ReplicatedStorage:WaitForChild("Remotes")
local placeBlockEvent = remotes:WaitForChild("placeBlockEvent")
placeBlockEvent.OnServerInvoke = (function(player, blockType, position1)
local crafted
local realStructure = buildings:FindFirstChild(blockType):Clone()
if realStructure then
realStructure.Position = position1
realStructure.Parent = workspace
crafted = true
else
crafted = false
end
return crafted
end)