hello, so I wanted to spawn a part based on the position of the mouse, only whenever I have the tool equipped. The thing is, i’ve already done it, and its working. However, its not working properly. I have these issues:
After activating the tool, its still spawning parts even I do NOT have the tool equipped.
Each click of the mouse, multiplies the part it spawns (ex. 1st click spawns 1 part, 2nd click spawns 2 parts, etc…)
this is the script (localscript in the tool)
local player = game.Players.LocalPlayer
local mouse = player.GetMouse()
local Remote = ReplicatedStorage.RemoteE
tool.Activated:Connect(function()
mouse.Button1Down:Connect(function()
Remote:FireServer(mouse.Hit)
end)
end)
(serverscript)
game.ReplicatedStorage.RemoteE.OnServerEvent:Connect(function(player, Position)
local Part = Instance.new("Part")
Part.Parent = player.Character
Part.CFrame = Position
end)
please, don’t provide me the scripts, im trying to learn lua. what i want is someone who would correct what is wrong in my code, and what should i do to correct that mistake. thank you in advance for helping!
unfortunately, i dont know what is a debounce. and no, after activating the tool, even if its unequipped (not a children of the Character), its still spawning parts.
every single time you equip a tool. This is why the script registers mouse click as many times as you equipped the tool, and executes Remote:FireServer(mouse.Hit) that many times.
If i were you, i’d check if the tool is equipped after the button is pressed.
you can call me dumb, but I changed my script into whatever I understood. What i did was I added
if tool.Equipped then
i’ve tried to place that right after the tool.activated, yet no avail. so i placed it after the mouse.Button1Down:Connect and its still doing those issues. i hope you wont mind to help me a little bit more, thank you so much!
Make sure to Anchor the part, if a part is not anchored it can be moved around which could lead to it falling in the void.
Another possible problem is this:
You are parenting the part to the player.
Edit:
This is the problem:
You should remove the mouse.Button1Down event, because tool.Activated runs when the player clicks.
Last but not least, make sure the tool has a part called Handle inside of it or set the property under the tool called RequiresHandle to false, if this is not enabled then the tool.Activated event wont run
Okay, it turns out we both confused tool.Activated with tool.Equipped.
Both of them are events. tool.Activated fires when you click while the tool is equipped, and tool.Equipped fires when you equip that tool.
Now that we know exactly when those events fire, we can go back to your code.
Every time tool.Activated fires, you connect Remote:FireServer(mouse.Hit) to mouse.Button1Down. Because you connect it again every time tool.Activated is fired, when mouse.Button1Down fires, Remote:FireServer(mouse.Hit) gets executed as many times as it got connected.
You can just completely get rid of mouse.Button1Down:Connect() because tool.Activated already fires when you click