Hello, this is my first time posting on the developer forums so sorry if I do this wrong. Also, I am still learning quite a bit but I love to challenge myself and find doing these really hard challenges I can learn a lot.
Anyway, I saw this post and its solution and thought I should give it a go since it seems like something I’d like to code and learn about. I started well and managed to get a fake blue semi-transparent wall to attach to the attachments on the foundation floor when a tool is equipped. You can see my code and the gif of what I mean below.
What I have so far (without the problem)
Picture of the attachments:
The Problem
The problem is when the player has the tool selected and the player clicks to place a wall it places multiple walls not only where the fake wall is but also on the example wall in the picture above. I found out that when the player clicks it repeats the function connected to MouseButton1Down a random amount of times. See below to see a gif of what happens. So far I have tried adding a debounce as I figured that should stop it firing multiple times but it hasn’t worked. My code will be below the gif. Any help would be greatly appreciated and I would love some feedback and ideas on what I should have done or should do to make this more efficient or just to solve problems.
My Code
So for this I have two scripts. One is a local script in the tool to detect when its equipped with the following code:
local RS = game:WaitForChild("ReplicatedStorage")
local ClickEvent = RS.PlacementSystem:WaitForChild("ClickToPlace")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ToolEquiped = false
local Tool = script.Parent
Tool.Equipped:Connect(function()
ToolEquiped = true
Main()
end)
function Main()
while ToolEquiped == true do
wait(0.1)
local WallPos
local MouseTarget = mouse.Target
local target
if MouseTarget then
target = MouseTarget.name
end
if target == "Foundation-Floor" then
local ClosestDistance = math.huge
local ClosestAttachment
for _,attachment in pairs(mouse.Target:GetChildren()) do
if attachment.Name == "WallSnapAttachment" then
local mousePos = mouse.Hit.p
local worldPos = attachment.WorldPosition
local distance = (mousePos - worldPos).Magnitude
if distance < ClosestDistance then
ClosestDistance = distance
ClosestAttachment = attachment
end
end
end
local floor = mouse.Target
local wall = game.Workspace.FakeWall
wall.Transparency = 0.75
local wallCframe = floor.CFrame *CFrame.new(ClosestAttachment.Position)
wallCframe = wallCframe *CFrame.new(-wall.FloorAttachment.Position)
wallCframe = wallCframe *CFrame.Angles(0, math.rad(ClosestAttachment.Orientation.Y), 0)
wall.CFrame = wallCframe
WallPos = wall.Position
else
local wall = game.Workspace.FakeWall
wall.Transparency = 1
end
Tool.Unequipped:Connect(function()
ToolEquiped = false
local wall = game.Workspace.FakeWall
wall.Transparency = 1
end)
local enabled = true
mouse.Button1Down:Connect(function()
if not enabled then return end
enabled = false
ClickEvent:FireServer(WallPos)
wait(0.1)
enabled = true
end)
end
end
The other is a small script is server storage to place the real wall when the player clicks. I have a remote function which works perfectly as I tested it and I am quite confident with them.
local RS = game:WaitForChild("ReplicatedStorage")
local ClickEvent = RS.PlacementSystem:WaitForChild("ClickToPlace")
ClickEvent.OnServerEvent:Connect(function(Player, WallTargetPosition)
local newWall = game.ServerStorage.Wall:Clone()
newWall.Parent = game.Workspace
newWall.Position = WallTargetPosition
end)
Again any help would be greatly appreciated and I would love some feedback and ideas on what I should have done or should do to make this more efficient or just to solve problems. Just so you know once I find and fix the bugs I am going to make this be able to place other foundations that snap together as well as other walls like windows and door frames similar to rust. This is not for an actual game but meant to be a learning experience for me.
Thanks for taking your time to read this (which is probably a mess so sorry)
Edit1: I know I should use comments I just haven’t developed that habit as I get excited when coding and tend to rush things a bit