Script doesn't work for some reason

title, I’m trying to make a script where, if you press the image button, you can place. blocks, but it doesn’t work for some reason.
script:

---------------------------------------------VARIBLES-------------------------------------------------
local remoteEvent = game.ReplicatedStorage:WaitForChild("BuildingSystemEvent(blueblock)")
local buttonOn = script.Parent.equipped
local runService = game:GetService("RunService")
local button = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local grid = 1.5
local blueBlock = game.ReplicatedStorage:WaitForChild("BlueBlock")
local shadow_block = blueBlock:Clone(); shadow_block.Transparency = .5; shadow_block.Parent = workspace.Blocks.ShadowBlocks; shadow_block.CanCollide = false
if shadow_block.CanCollide == true then
	print("BRUH")
end

mouse.TargetFilter = shadow_block

---------------------------------------------FUNCTIONS-------------------------------------------------

function snapToGrid(x)
	return math.floor(x/grid + 0.5)*grid
end

function calculateY(toP, toS, oS)
	return (toP + toS * 0.5) + oS * 0.5
end

if buttonOn.Value == false then
	print("hehe printing stupid things is funny")
	button.Activated:Connect(function()
		buttonOn.Value = true
		remoteEvent:FireServer("blueBlock", shadow_block.Position)
	end)
else
	buttonOn.Value = false
	print("kekw")
end

if buttonOn == true then
	runService.RenderStepped:Connect(function()
		if mouse.Target ~= nil then
			shadow_block.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
			buttonOn.Value = false
			end
		end)
if buttonOn.Value == false then
	shadow_block.Transparency = 1
else
	buttonOn.Value = true
	shadow_block.Transparency = 0.5
	end
end

script inside ServerScriptService:


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

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)

it must be the buttonOn and if buttonOn problem, try putting them if statement to a loop and also its better if u put the renderStepped outside the if statement.

maybe this will work?

---------------------------------------------VARIBLES-------------------------------------------------
local remoteEvent = game.ReplicatedStorage:WaitForChild("BuildingSystemEvent(blueblock)")
local buttonOn = script.Parent.equipped
local runService = game:GetService("RunService")
local button = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local grid = 1.5
local blueBlock = game.ReplicatedStorage:WaitForChild("BlueBlock")
local shadow_block = blueBlock:Clone(); shadow_block.Transparency = .5; shadow_block.Parent = workspace.Blocks.ShadowBlocks; shadow_block.CanCollide = false
if shadow_block.CanCollide == true then
	print("BRUH")
end

mouse.TargetFilter = shadow_block

---------------------------------------------FUNCTIONS-------------------------------------------------

function snapToGrid(x)
	return math.floor(x/grid + 0.5)*grid
end

function calculateY(toP, toS, oS)
	return (toP + toS * 0.5) + oS * 0.5
end

button.Activated:Connect(function()
	print("hehe printing stupid things is funny")
	buttonOn.Value = true
	remoteEvent:FireServer("blueBlock", shadow_block.Position)
end)

runService.RenderStepped:Connect(function()
	if not buttonOn then 
		shadow_block.Transparency = 1
		return
	else
		buttonOn.Value = true
		shadow_block.Transparency = 0.5
	end
	if mouse.Target ~= nil then
		shadow_block.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
		buttonOn.Value = false
	end
end)
2 Likes

Ok so i tried that, but there are 2 problems; 1. the y axis is a little funky:


2. I don’t want it to place blocks when i hit the button a second time, i just want to make it so, if i hit the button, i am able to place the blocks by left clicking where i want it to be placed. and then if i dont want to place blocks, i want to be able to click the button again to remove the block placement.

1 Like
  1. ow, im not too experienced on mouse thingy but u should try to use raycasting (maybe)

  2. u could make a variable to see current player object(the object tha players want to spawn/place), and use UIS(User Input Service) to detect if player is left clicking or not, and when players lc fires the remote with the current object

1 Like

Could you help me with that? I dont know how to do it.

You can use RayCast, to check if there’s anything below the block’s vector, so it can stick to it, I can make a script if you want

1 Like

Please do, I have no idea how to use raycasting.

Ok, do you want the block to stick when clicked? and stay in the position where it was clicked ?

1 Like

yes, please
(character limit sucs)

Okay, wait here, I’ll get this done.

alright, thank you
(i HATE CHARACYER LIMITS)

Okay, never mind, I tried to do what you mean, but it’s easier for me to show you what you mean:

This script is just a test for you to learn how “Placement” really works, if you want to learn more about it, there is already a module ready to be used, Creating A Furniture Placement System

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")

local MaxDistance = 0.01 --["RenderDistanceBox 0. 0. 1. X"] [ "Recommend 0.01" ]

local DummyBlock = game.ReplicatedStorage.Block:Clone()
DummyBlock.Parent = workspace
DummyBlock.Transparency = 0.5
DummyBlock.CanCollide = false

local function roundToStep(initialValue,step)
	local roundedValue = math.floor(initialValue/step)*step
	return roundedValue
end

RunService.RenderStepped:Connect(function()
	DummyBlock.Position = Vector3.new(roundToStep(Mouse.Hit.Position.X, MaxDistance), 1, roundToStep(Mouse.Hit.Position.Z, MaxDistance))
end)

Mouse.Button1Down:Connect(function()
	local Block = game.ReplicatedStorage.Block:Clone()
	Block.Parent = workspace
	Block.Position = Vector3.new(roundToStep(Mouse.Hit.Position.X, MaxDistance), 1, roundToStep(Mouse.Hit.Position.Z, MaxDistance))
end)

Block.rbxm (4,0,KB)

1 Like

The defect of RayCast is that it is very difficult to get caught on the Mouse, so study a little about RayCast: Raycasting | Roblox Creator Documentation

To be better than everyone else

Or If you want, try to check some videos to see how you want

1 Like

Hey! I tried the code in your first post (I obvoiusly tweaked some of the code), and it didn’t work. Could you help me with that? Also I read the entire thing of your link, but to no avail, I couldn’t figure how to incorporate into my script.