So I created this function to fire a missile and then fire the second missile if the function was called again but it only seems to fire the first missile then ceases to work
Bool function not working?
2 answers
function fire() -- Pew pew
local mWasMissileFired = false
if mWasMissileFired == false then
mWasMissileFired = true
main.Parent.Safe.Missiles.Missile:BreakJoints()
wait (0.05)
main.Parent.Safe.Missiles.Missile.Launch.Disabled = false -- This is not a good way to run scripts
else
print 'Worked!'
main.Parent.Safe.Missiles.Missile2:BreakJoints()
wait (0.05)
main.Parent.Safe.Missiles.Missile2.Launch.Disabled = false -- This is not a good way to run scripts
mWasMissileFired = false
end
end
I recommend putting the whole launch missile script as a separate function, then add a plethora of boolean variables (assuming you only want the 2 missiles to fire and those 2 missiles only. You could add a regen function to restart the whole process):
function Launch()
--[[ Your launch function here ]]
end
function Launch2()
--[[ Your launch function here ]]
end
function fire() -- Pew pew
local mWasMissileFiredOnce = false
local mWasMissileFiredTwice = false
local debounce = true
if mWasMissileFiredOnce == false and mWasMissileFiredTwice == false and debounce == true then
debounce = false
main.Parent.Safe.Missiles.Missile:BreakJoints()
wait (0.05)
Launch()
mWasMissileFiredOnce = true
debounce = true
elseif mWasMissileFiredOnce == true and mWasMissileFiredTwice == false and debounce == true then
debounce = false
print 'Worked!'
main.Parent.Safe.Missiles.Missile2:BreakJoints()
wait (0.05)
Launch2()
mWasMissileFiredTwice = true
debounce = true
elseif mWasMissileFiredOnce == true and mWasMissileFiredTwice == true then
return nil -- Ehh, this last 'elseif' statement isn't really needed, but oh well
end
end
But assuming that you don't want a regen function, that you wanted to reload the missiles after a set amount of time or something. Well, you'd have to get rid of the BreakJoints() method and instead make a new part as a missile.
mWasMissileFiredOnce = false
mWasMissileFiredTwice = false
debounce = true
FinishedShooting = script.Parent.FinishedShooting
FinishedShooting.Value = false
-- Setting these variables to global (outside of a code block) will let other functions manipulate the variables
function Launch()
--[[ Your launch function here ]]
end
function Launch2()
--[[ Your launch function here ]]
end
function fire() -- Pew pew
if mWasMissileFiredOnce == false and mWasMissileFiredTwice == false and FinishedShooting == false and debounce == true then
debounce = false
main.Parent.Safe.Missiles.Missile:BreakJoints()
wait (0.05)
Launch()
mWasMissileFiredOnce = true
debounce = true
elseif mWasMissileFiredOnce == true and mWasMissileFiredTwice == false and FinishedShooting == false and debounce == true then
debounce = false
print 'Worked!'
main.Parent.Safe.Missiles.Missile2:BreakJoints()
wait (0.05)
Launch2()
mWasMissileFiredTwice = true
debounce = true
FinishedShooting = true
elseif mWasMissileFiredOnce == true and mWasMissileFiredTwice == true then
return nil -- Ehh, this last 'elseif' statement isn't really needed, but oh well
end
end
FinishedShooting.Changed:connect(function (Value)
if FinishedShooting.Value == true then
wait(10)
mWasMissileFiredOnce = false
mWasMissileFiredTwice = false
FinishedShooting.Value = false
end
end)
-- For this to work, you have to have a bool value named as "FinishedShooting" with it set to false at the parent of this script.
1 comment
So I used your first option as the second one ended up breaking the rest of the script (Don't know exactly why, think it was something regarding the Boolean not being local) but for some reason it just keeps running the Launch() function and doesn't move onto Launch2() new code can be found below.
So I used your first option as the second one ended up breaking the rest of the script (Don't know exactly why, think it was something regarding the Boolean not being local) but for some reason it just keeps running the Launch() function and doesn't move onto Launch2()
*Note chached "Missile" to "Missile1" dissolved "Launch" script & merged with Launch() functions
function Launch()
wait (0.2)
main.Parent.Safe.Missiles.Missile1.Fired:Play()
local a = Instance.new('BodyThrust')
a.force = Vector3.new(0, 4105, 125000)
a.location = Vector3.new(0, 0, 0)
a.Parent = main.Parent.Safe.Missiles.Missile1
wait (0.5) -- Safety percaution to avoid self destruction
main.Parent.Safe.Missiles.Missile1.Explode.Disabled = false
end
function Launch2()
wait (0.2)
main.Parent.Safe.Missiles.Missile2.Fired:Play()
local a = Instance.new('BodyThrust')
a.force = Vector3.new(0, 4105, 125000)
a.location = Vector3.new(0, 0, 0)
a.Parent = main.Parent.Safe.Missiles.Missile2
wait (0.5) -- Safety percaution to avoid self destruction
main.Parent.Safe.Missiles.Missile2.Explode.Disabled = false
end
function fire() -- Pew pew
local mWasMissileFiredOnce = false
local mWasMissileFiredTwice = false
local debounce = true
if mWasMissileFiredOnce == false and mWasMissileFiredTwice == false and debounce == true then
debounce = false
main.Parent.Safe.Missiles.Missile1:BreakJoints()
wait (0.05)
Launch()
mWasMissileFiredOnce = true
debounce = true
print 'Done1'
elseif mWasMissileFiredOnce == true and mWasMissileFiredTwice == false and debounce == true then
debounce = false
print 'Worked!'
main.Parent.Safe.Missiles.Missile2:BreakJoints()
wait (0.05)
Launch2()
mWasMissileFiredTwice = true
debounce = true
print 'Done2'
elseif mWasMissileFiredOnce == true and mWasMissileFiredTwice == true then
return nil -- Ehh, this last 'elseif' statement isn't really needed, but oh well
end
end
1 comment
Do not use the Answers to reply to somone; It signifies that the Question has been answer, use Comments instead.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Please post your code in a Lua block in your question.