Spawning a part according to mouse position

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:

  1. After activating the tool, its still spawning parts even I do NOT have the tool equipped.

  2. 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!

5 Likes

I dont get how the parts are spawning multiples times after each click, have you tried some sort of debounce?

Also check if the player has the tool in their character?

1 Like

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.

1 Like

adding to this, i also didn’t see any errors in the console

1 Like

This code binds a function to an event. In other words, if Event happens, something gets executed.

Event:Connect(function()
   --something
end)

tool.Activated fires every time you equip a tool (EDIT: this is false, check out my other reply), so you execute this

mouse.Button1Down:Connect(function()
   Remote:FireServer(mouse.Hit)
end)

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.

3 Likes

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!

1 Like

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

1 Like

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

1 Like

i just got back to try it and it solved my problem! thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.