Block Placement Error

Hey Devs,

I am trying to create a Block Placement System but my script has and ERROR, I think.

Here is my code:

Local Script:

local GrassBlock = game.StarterPack:WaitForChild("Grass")
local DirtBlock = game.StarterPack:WaitForChild("Dirt")
local StoneBlock = game.StarterPack:WaitForChild("Stone")
local PlaceBlock = game.ReplicatedStorage.PlaceBlock

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouseclick = false

GrassBlock.Equipped:Connect(function()
	mouse.Button1Up:Connect(function()
		mouseclick = true
		PlaceBlock:FireServer()
	end)
end)

Main Script:

local PlaceBlock = game.ReplicatedStorage:WaitForChild("PlaceBlock")
local GrassBlock = game.ReplicatedStorage.GrassBlock
local mouse = game.Players.LocalPlayer

PlaceBlock.OnServerEvent:Connect(function()
	local GrassNew = GrassBlock:Clone()
	GrassNew.CFrame = CFrame.new(mouse.Hit.X, mouse.Hit.Y)
end)

All help appreciated!

Why did you delete it? Is there a problem?

Instead of doing Equipped, then running a Button1Up event, won’t work:

Do GrassBlock.Activated instead, then fire the remote;

Try including a parameter of your mouse’s position in the FireServer function;

On the server script, add a new parameter called player, and a second parameter called xyz;

Set GrassNew.CFrame to CFrame.new(xyz);

Still doesn’t work
Here is the updated scripts:

Local:

local GrassBlock = game.StarterPack:WaitForChild("Grass")
local DirtBlock = game.StarterPack:WaitForChild("Dirt")
local StoneBlock = game.StarterPack:WaitForChild("Stone")
local PlaceBlock = game.ReplicatedStorage.PlaceBlock

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

GrassBlock.Activated:Connect(function()
	PlaceBlock:FireServer(mouse.X, mouse.Y, mouse.Z)
end)

Main:

local PlaceBlock = game.ReplicatedStorage:WaitForChild("PlaceBlock")
local GrassBlock = game.ReplicatedStorage.GrassBlock


PlaceBlock.OnServerEvent:Connect(function(player, xyz)
	local GrassNew = GrassBlock:Clone()
	GrassNew.CFrame = CFrame.new(xyz)
end)

PlaceBlock:FireServer(mouse.Hit.Position)

It still doesn’t work:

GrassBlock.Activated:Connect(function()
	PlaceBlock:FireServer(mouse.Hit.Position.X, mouse.Hit.Position.Y, mouse.Hit.Position.Z)
end)

Literally copy and paste this:
PlaceBlock:FireServer(mouse.Hit.Position) DONT MODIFY ANYTHING

I did that but it still didn’t work!

Its because your saying that

PlaceBlock:FireServer(mouse.Hit.Position.X, mouse.Hit.Position.Y, mouse.Hit.Position.Z)

are three different arguments, and in

PlaceBlock.OnServerEvent:Connect(function(player, xyz)

your saying xyz is mouse.position.X, so just either do what @Outlua said or create 3 different arguments like so;

PlaceBlock.OnServerEvent:Connect(function(player, x, y, z)

There are a couple of issues with your code that are causing it to not work as expected.

First, in the local script, you are defining the PlaceBlock variable as a reference to the PlaceBlock object in the ReplicatedStorage service, but you never use it. Instead, you are using the mouse.Button1Up event to detect when the player clicks the mouse button and then setting the mouseclick variable to true . This variable is never used, so it does not affect the behavior of the script.

To fix this issue, you can remove the mouseclick variable and use the PlaceBlock variable to call the FireServer method when the player clicks the mouse button. Here is an example of how you could modify your local script to do this:

local GrassBlock = game.StarterPack:WaitForChild("Grass")
local PlaceBlock = game.ReplicatedStorage.PlaceBlock

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

GrassBlock.Equipped:Connect(function()
  mouse.Button1Up:Connect(function()
    PlaceBlock:FireServer()
  end)
end)

In the main script, you are defining the PlaceBlock variable as a reference to the PlaceBlock object in the ReplicatedStorage service, but you never use it. Instead, you are using the GrassBlock variable to create a new GrassBlock object, but this variable is never defined or initialized. As a result, the Clone method will fail and the script will not work as expected.

To fix this issue, you can update the main script to use the PlaceBlock variable instead of the GrassBlock variable. Additionally, you should add a parameter to the OnServerEvent callback function to receive the data that is sent by the local script. This data can then be used to determine which type of block to place. Here is an example of how you could modify the main script to do this:

local PlaceBlock = game.ReplicatedStorage:WaitForChild("PlaceBlock")
local mouse = game.Players.LocalPlayer

PlaceBlock.OnServerEvent:Connect(function(blockType)
  local newBlock = game.StarterPack:WaitForChild(blockType):Clone()
  newBlock.CFrame = CFrame.new(mouse.Hit.X, mouse.Hit.Y)
end)

With these changes, the local script will send the type of block to place to the main script when the player clicks the mouse button. The main script will then use this information to determine which block to place, and will clone the appropriate block and set its position to the location where the player clicked. This should allow the block placement system to work as expected.

This is not working LOL. It’s basically the same script but with less data.

The problem here is I do not understand the error, but based on the code you provided, there are a few issues with your script.

In your local script, the mouseclick variable is not defined, so it will throw an error. You should define the mouseclick variable like this:

local mouseclick = false

In your main script, the GrassBlock variable is not defined. It should be defined like this:

local GrassBlock = game.ReplicatedStorage:WaitForChild("GrassBlock")

Another issue is that you are trying to clone the GrassBlock object, but it does not have a Clone method. You should instead use the Instance.new method to create a new instance of the GrassBlock object, like this:

local GrassNew = Instance.new("Model", game.Workspace)
GrassNew.Name = "GrassBlock"

Finally, in your main script, you are using the CFrame.new method to set the position of the GrassNew object, but you are not providing the proper arguments. The CFrame.new method takes three arguments: the X, Y, and Z coordinates. Your code should look like this:

local GrassNew = Instance.new("Model", game.Workspace)
GrassNew.Name = "GrassBlock"
GrassNew.CFrame = CFrame.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z)

I hope this helps! Let me know if you have any other questions.

1 Like