local RE = game.ReplicatedStorage.RemoteEvent
local FR = script.Parent.Frame
local Buildings = game.ReplicatedStorage.Buildings
local stuff = game.Workspace.Stuff
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local building = false
FR.ImageButton.MouseButton1Click:Connect(function()
local block = FR.ImageButton.Value.Value
block.Parent = stuff
building = true
mouse.Button1Down:Connect(function()
building = false
RE:FireServer(block,mouse.Hit)
block.Parent = game.ReplicatedStorage.Buildings
end)
local function move()
while building == true do
print(building)
block.CFrame = CFrame.new(mouse.Hit.X,0, mouse.Hit.Z)
wait()
end
end
if building == true then
move()
end
end)
Server code:
local RE = game.ReplicatedStorage.RemoteEvent
RE.OnServerEvent:Connect(function(plr,block,mouse)
print("RemoteEvent Fired")
local block2 = block:Clone()
block2.Parent = workspace.Stuff
block2.CFrame = CFrame.new(mouse.X,0,mouse.Z)
end)
because you’re creating a new block in both the client and serverside
client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RE = ReplicatedStorage.RemoteEvent
local FR = script.Parent.Frame
local Buildings = ReplicatedStorage.Buildings
local stuff = workspace.Stuff
local plr = Players.LocalPlayer
local mouse = plr:GetMouse()
local building = false
local currentBlock = nil
local function moveBlock()
while building and currentBlock do
currentBlock.CFrame = CFrame.new(mouse.Hit.X, 0, mouse.Hit.Z)
task.wait()
end
end
local function startBuilding(block)
currentBlock = block:Clone()
currentBlock.Parent = stuff
building = true
task.spawn(moveBlock)
end
local function stopBuilding()
if currentBlock then
building = false
RE:FireServer(currentBlock.Name, mouse.Hit)
currentBlock:Destroy()
currentBlock = nil
end
end
FR.ImageButton.MouseButton1Click:Connect(function()
if not building then
local blockModel = FR.ImageButton.Value.Value
startBuilding(blockModel)
end
end)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 and building then
stopBuilding()
end
end)
serverside
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Buildings = ReplicatedStorage.Buildings
local RE = ReplicatedStorage.RemoteEvent
RE.OnServerEvent:Connect(function(plr, blockName, mouseHit)
local blockModel = Buildings:FindFirstChild(blockName)
if blockModel then
local newBlock = blockModel:Clone()
newBlock.Parent = workspace.Stuff
newBlock.CFrame = CFrame.new(mouseHit.X, 0, mouseHit.Z)
end
end)
Sure No Issues
So in the old code every time you placed a bvlock it would add an extra one so if you tried to place one block you’d end up with two in the game
The reason of it because the block was being created as soon as you clicked the button and same block was used for both preview and placement as well the server was receiving and cloning an existing block which could cause deuplication
as a solution on the client side we create a temporary copy for preview and then we delete the temporary block after placement
on the server side we create a new block based on the name instead of cloning an existing one