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.