Ad
Log in to vote
0

How do I prevent this script from running multiple times?

Asked by 4 years ago
Edited 4 years ago

How do I prevent this script from running multiple times (effects it has)

It makes tons of BodyVelocity in the same part, I also need to destroy the body velocity after x amount of second but dont know exactly how, can anyone fix it making tons of Bodyvelocity in the same part? Thanks.

I also tried to use debounce but that just slowed down the rate which makes more BodyVelocity in the same part.

script.Parent.Touched:Connect(function(touched)
    if touched.Name == "EF2" or touched.Name == "EF1" then
        local force = Instance.new("BodyPosition",touched)
        local noforce = script.Parent.Parent.destroy
        local noforceclone = force:Clone().Parent == force
        force.MaxForce = Vector3.new(4000,4000,4000)
        force.Parent = touched
        force.Position = script.Parent.Parent.debripos.Position
    end
end)
0
run a debounce until the part has left the part TheluaBanana 946 — 4y
0
also isnt this abt the bouncy thing? TheluaBanana 946 — 4y
0
are you not able to read? i literally just said i tried to use a debounce and it doesnt work. LoganboyInCO 150 — 4y
0
Try maybe adding a FindFirstChild or WaitForChild to check if there is a BV, and if there is, set a debounce that prevents it to add anymore BV. If there isnt BV set the debounce to false so it can add BV. HeyItzDanniee 252 — 4y
View all comments (2 more)
0
@LoganboyInCO i am able to read relatively fluently, like a normal human. But I would appreciate if u could read the last few words of what i said(like a normal human) and take it into consideration instead of seeing a "debounce" in my sentence and straight up criticizing it(aka ask me wydm by "part has left the part") TheluaBanana 946 — 4y
0
also what do u intedn to do with this? TheluaBanana 946 — 4y

1 answer

Log in to vote
2
Answered by 4 years ago

I've been trying to use the disconnect function somewhere in the script, but I can't find out how to make that work. The only thing I can think of is not letting the script run unless :FindFirstChild("Force") comes back as nil. This would make your script look like this:

script.Parent.Touched:Connect(function(touched)
    local check = touched:FindFirstChild("Force")
    if touched.Name == "EF2" or touched.Name == "EF1" and not check then
        local force = Instance.new("BodyPosition")
        force.Parent = touched -- took this out of the parentheses, that method of parenting is deprecated.
        local noforce = script.Parent.Parent.destroy
        local noforceclone = force:Clone().Parent == force
        force.MaxForce = Vector3.new(4000,4000,4000)
        force.Parent = touched
        force.Position = script.Parent.Parent.debripos.Position
    force.Name = "Force"
    wait(x)
    force:Destroy
    end
end)

I know this is almost the same as a debounce, but give it a shot before you shoot it down.

-Cmgtotalyawesome

0
damn "give it a shot before u shoot it down" TheluaBanana 946 — 4y
0
Good, i did not know that Instance.new("CLASSNAME", parent) is deprecated lmao yHasteeD 1819 — 4y
0
Small little mistake on line 13, you forgot the () for :Destroy(), and it should only put the force into it once, but overall it works very well, accepted. LoganboyInCO 150 — 4y
0
lol TheluaBanana 946 — 4y
Ad

Answer this question