It appears i’m having trouble with the mouse.Button1Down:Connect function. I’m making a script to manage a placement system for a game. It works great except I have noticed that when the user places the object it repeats the function multiple times. This was fine in the early stages of development because the only effect was something being printed multiple times and occasional lag.
Then I started working on passing data from the client to the server because its all contained in a local script inside ReplicatedFirst and I need other players to see the placed part. Due to this repeating glitch or error in the code it sends the data to the server multiple times resulting in more than the desired amount of parts being placed.
if anyone knows of this issue or sees a problem in my code it would be greatly appreciated.
Problem code:
mouse.Button1Down:Connect(function()
if placeable == false then
part:Destroy()
part2:Destroy()
if towersPlaced == false then
towersPlaced = false
end
else
local tower = "Test"
local locX = part.Position.X
local locY = part.Position.Y
local locZ = part.Position.Z
print "placed"
Clicked = true
towersPlaced = true
part2.Parent = game.workspace.placedTowers
--send location and name to server script
game.ReplicatedStorage.PlaceObject:InvokeServer(locX,locY,locZ,tower)
part:Destroy()
part2:Destroy()
print(part.Position)
end
--wait function prevents multi placement within local script and not within server script
wait (.0001)
end)
Whenever I place the object it prints “placed” about 15 times instead of once
Is this a known issue? I’m moderately experienced with scripting but this is my first time embarking on a large project involving client to server interactions and similar.
If you need to know it is contained inside another mouse.Button1Down function for a TextButton.
I apologize for the messy code I’m not the most organized person but at least it works.
I can provide the rest of the code if needed its just kinda long and I dont want to overcomplicate things.
How are you connecting the event? There’s no way for Button1Down to activate more than once when you press left click unless you’re connecting it more than once
I’ve looked through my script but couldnt seem to find anything that would be causing it to execute multiple times. The only possibility would be this for loop but the button1down function isnt contained inside of it. however it does have some influence because it determines if the part can be placed on the surface its touching or not
for i,v in next, part2:GetTouchingParts() do
if (v.Parent == game.Workspace.UnplaceableAreas or v.Parent == game.workspace.placedTowers) then
part.Color = Color3.new(1, 0, 0)
print "dont place"
placeable = false
else
part.Color = Color3.new(0, 1, 0)
placeable = true
end
end