How do I make this building script work?

Context for what the script does: I’m trying to make a script, where if you press this TextButton called “Blue”, a BooleanValue will become true (this boolean value being named “equipped”), and this will enable you to place blocks, however, if you press the button again while equipped is true, you stop placing blocks.

Here’s the scripts/screenshots:
Main local script:

-- VARIBLES --
local event = game:GetService("ReplicatedStorage"):WaitForChild("BuildingSystemEvent")
local button = script.Parent
local buttonEquipped = script.Parent.equipped
local buttonOwned = script.Parent.Owned
local rS = game:GetService("RunService")

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

local grid = 1.5
local blueBlock = game:GetService("ReplicatedStorage"):WaitForChild("BlueBlock")
local shadowBlock = blueBlock:Clone(); shadowBlock.Transparency = 0.5; shadowBlock.Parent = game:GetService("Workspace"):WaitForChild("Blocks"):WaitForChild("ShadowBlocks"); shadowBlock.CanCollide = false

mouse.TargetFilter = shadowBlock
if shadowBlock.CanCollide == true then
	print("ShadowBlock(BLUE)'s CanCollide property = true")
	shadowBlock.CanCollide = false
end

-- FUNCTIONS --

local function snapToGrid(x)
	return math.floor(x/grid + 0.5)*grid
end
local function calculateY(toP, toS, oS)
	return (toP + toS * 0.5) + oS * 0.5
end

if buttonOwned == true then
	if button.Activated then
		buttonEquipped = true
		if buttonEquipped == true then
			event:FireServer("blueBlock", shadowBlock.Position)
		end
	end
end

rS.RenderStepped:Connect(function()
	if buttonEquipped == false then
		shadowBlock.Transparency = 1
		return
	else
		buttonEquipped = 0.5
		shadowBlock.Transparency = 0.5
	end
	if mouse.Target ~= nil then
		shadowBlock.Position = Vector3.new(snapToGrid(mouse.Hit.Position.X), calculateY(mouse.Target.Position.Y, mouse.Target.Size.Y, blueBlock.Size.Y), snapToGrid(mouse.Hit.Position.Z))
	else
		buttonEquipped = false
	end
end)

Server script in ServerScriptService:

local remoteEvent = game.ReplicatedStorage:WaitForChild("BuildingSystemEvent")

remoteEvent.OnServerEvent:Connect(function(player, block, position)
	print(position)
	local clone = game.ReplicatedStorage:WaitForChild("BlueBlock"):Clone()
	clone.Parent = workspace.Blocks.BlueBlocks
	clone.Position = position
end)

screenshot of screengui:

1 Like