Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

i want a part keep spawning when i hold mouse but idk how?

Asked by 5 years ago

So here it is the script: ```local Player = game.Players.LocalPlayer

local Char = Player.Character

local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()

if Mouse.Target ~= nil and Mouse.Target.Name == "Baseplate" and game.Workspace:FindFirstChild("Baseplate") then

local part = Instance.new("Part",game.Workspace)

part.CFrame = Mouse.Hit

part.Anchored = true

part.CanCollide = false

end

end)`` i want a part keep spawning when i hold the mouse, anyone know how to do this? (Sorry for my bad english)

0
Use a boolean value set to false outside of the function, make it true inside the function then make a while loop for while the boolean is true. You would also have to create a separate function to set the boolean back to false. DeceptiveCaster 3761 — 5y
0
Thanks dude its working Realdava 2 — 5y
0
oh, i was kind of late xd well i hope i helped starmaq 1290 — 5y
0
funfact: your question is the 78000th, gg i guess xd starmaq 1290 — 5y

1 answer

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago
Edited 5 years ago

I'm assuming you want the part to be always spawning as long as you hold the mouse button, so it's like a hold the button thing, and it will stop when you stop holding. Well, ROBLOX sadly doesn't have an event for that, so you'd want to use some scripting knowledge as MCAndRobloxUnited said. So before making the script, let me explain, since we're gonnna have the part always spawning infinitly as long as we're holding, we are obviously gonna need a while loop. Inside the event. Now, if we add that and press the button it will start instancing (the same word as spawning) parts infinitly, but it will always go even if we stop holding. And we don't want that. We can use the Button1Up event which fires when you stop pressing the mouse button and use break to stop the loop. But that is a bit unnefficent. So, we would use some boolean values magic! Have a variable set to false. When you click the mouse button it will turn to true, and the while loop will have a condition if the variable was true. Since we clicked it will turn to true, so it will start looping, and now we'll use Button1Up to trun that value to false, and like that the loop will stop looping. Got it? That's it, it's just a bit of logic. Now why I said that using Button1Up earlier is unnefficent because since if we went that way we'll be using break and that break will need to be inside the loop, which means we need the event to be inside the loop too. And that also means we'd have the Up event inside the Down event, that is a bit stupid. Always try your best to avoid an event inside an event. Now this is the script:

local plr = game.Players.LocalPlayer
local on = false
local mouse = plr:GetMouse()

mouse.Button1Down:Connect(function()

on = true
while on do
wait(0.1) --change this to whatever you want
if mouse.Target ~= nil and mouse.Target.Name == "Baseplate" and game.Workspace:FindFirstChild("Baseplate") then
local part = Instance.new("Part")
part.Parent = game.Workspace
part.CFrame = mouse.Hit
part.Anchored = true
part.CanCollide = false
end

end
end)



mouse.Button1Up:Connect(function()
on = false
end)

I fully tested this and it works!

!result

This part has no relation with your question, just general learning

Now, a couple of things that I want to point out, first a don't use the Instance.new(class, parent) second paramater, which is the parent that you want to sit the instanced object to. It is deprecated (which means an old outdated thing) but you can still use it. You gotta set its parent how you would do it normally, they say that's better due to some performance issues. Second, you can tell I did while on do and not check if on == to true by doing while on == true do. Now that's that same thing. The condition inside the while loop will determind if the loop loop or not. If the condition returns false or nil it will not loop. If it returns a non nil value (like numbers and strings so pretty much anything that exists) or a true value it will loop. So technaclly if we type true inside there it will loop because that's true. And on is equal to true, so it is pretty much true. And on == true will work too and that's for a reason. As we said on is equal to true so it is the same as true == true. Try printing true == true or anything that's equal to something else like 5 == 5. It will print true, cuz those 2 things are equal to eachother, so it is logiclly a true statment. That's the idea. "hi" = "pizza" will be false cuz those 2 strings aren't equal to eachother. Other things return true or false such as certain function, like :IsA() and :FindFirstChild(). They will return true if what you asked them to check is true. So as I said printing for example print(part:IsA("Part') that will print true, cuz that part has a class of Part, which is right so it's true.

--These things will print true
print(8 == 8)
print(true)
--But for example this function wouldn't return true because it didn't find the child (:FindFirstChild() returns two things; the instance we want to find, and a boolean value to determind if that child was found)
print(workspace:FindFirstChild("Player') --prints false

if 5 then
print("njdsg")
--this will work since as i said that condition will work too if the value is non-nil
end

I know that I opened a really big but boring thing that has no realtion to your question and littearly bigger then the answer xd, but hay learning is cool!

0
Approved. DeceptiveCaster 3761 — 5y
0
Approved.[2] starmaq 1290 — 5y
0
^ Fad99 286 — 5y
0
Approved.[3] Realdava 2 — 5y
0
;) starmaq 1290 — 5y
Ad

Answer this question