Here's my problem, the script works, but when you click a block (the FireServer) deletes the block that you click and creates new bricks around it. You have to move the mouse for the function to run again, is there a way where I wouldn't have to do that?
--Variables local blockValues = game.ServerStorage.Configuration.block_Values local player = game.Players.LocalPlayer local mouse = player:GetMouse() local target local selected_Block = "None" local selected_Position local equipped = false --Functions function checkBlock(pos) game.ReplicatedStorage.createBlock:FireServer(pos) end --MainScripts script.Parent.Equipped:connect(function() equipped = true mouse.Move:connect(function() if equipped then if mouse.Target then target = mouse.Target if target:FindFirstChild("Type") then if selected_Block == tostring(mouse.Target.Name) then print("Same Block!") else print("Different Block!") selected_Block = mouse.Target.Name selected_Position = mouse.Target.Position end end end end end) end) script.Parent.Unequipped:connect(function() equipped = false end) mouse.Button1Down:connect(function() if equipped then if mouse.Target:FindFirstChild("Type") then checkBlock(selected_Position) end end end)
The reason this is happening is that your block detection function only happens when the mouse is moved.
One possible solution for this is creating a local function blockSelection()
or something of the sort, and put everything inside mouse.Move:connect()
as the function. (Also, change connect
to Connect
. The former is deprecated.)
Then, you can call the function when you do Button1Down
too.
local function blockSelection() if equipped then if mouse.Target then target = mouse.Target if target:FindFirstChild("Type") then if selected_Block == tostring(mouse.Target.Name) then print("Same Block!") else print("Different Block!") selected_Block = mouse.Target.Name selected_Position = mouse.Target.Position end end end end end mouse.Move:Connect(blockSelection) mouse.Button1Down:Connect(function() if equipped then if mouse.Target:FindFirstChild("Type") then checkBlock(selected_position) blockSelection() end end end)
There could be - and probably is - better ways to solve your problem, but here's one of the solutions.
Hope it helps :)