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

Why doesn't "If deploymech then" work?

Asked by 9 years ago

I made this script, when you click a button it spawns a car; when you die the car stays, but if you spawn a new car the old one is removed, but when I click the button the second time, it doesn't delete the old car, does this mean the "If deploymech then" isn't working? [Original script]

    deploymech = script.Parent:FindFirstChild("DeployMech")


    function onClicked()
        print("Clicked!")
        if deploymech then
            print("about to remove!")
            wait(1)
            deploymech:remove()

        end
    end

script.Parent.Panel.ChooseMech.ClickDetector.MouseClick:connect(onClicked)

[Finished script]

    function onClicked()
        print("Clicked!")
        if script.Parent.Panel.IsMechValue.Value == 0 then
            local deploymech = script.Parent:FindFirstChild("DeployMech")
                if deploymech then
                print("about to remove!")
                deploymech:remove()
            end

        end
    end


script.Parent.Panel.ChooseMech.ClickDetector.MouseClick:connect(onClicked)

This might be an easy fix that I am over looking! Please help!!! Thank you!!! P.S This is a regular script

1
Why are you using repeat? marcoantoniosantos3 200 — 9y

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Your Problem

Your deploymech variable is outside the scope of the function! This means that 'deploymech' will only ever be the object found the first time - hence when it's destroyed then the next one will never get destroyed. You need to define the variable inside the function!

Code

function onClicked()
    --Define the variable INSIDE the function
    local deploymech = script.Parent:FindFirstChild("DeployMech")
    if deploymech then
        local newVar = deploymech:Clone()
        wait(1)
        deploymech:Destroy() --'Remove' is deprecated!
        --Changing the name of the variable is redundant.
        newVar.Parent = workspace
        newVar:MakeJoints()
    end
end

script.Parent.Panel.ChooseMech.ClickDetector.MouseClick:connect(onClicked)
Ad
Log in to vote
1
Answered by 9 years ago

EDITED

I've noticed you are using repeat. No reasson for so.

Also, 9+10=19 and not 21, wich means you run it once and it stops.

So remove the repeat and you are done.

    wait(0.001)
    local deploymech = script.Parent:FindFirstChild("DeployMech")


    function onClicked()
        print("Clicked!")
        if deploymech then
            print("about to remove!")
            local newVar=deploymech:Clone()
            wait(1)
            deploymech:remove()
            deploymech=newVar
            deploymech.Parent=workspace
            deploymech:MakeJoints()
        end
    end

script.Parent.Panel.ChooseMech.ClickDetector.MouseClick:connect(onClicked)

Hope this helps! Thanks, ~marcoantoniosantos3

0
The repeat thing now lets it click and print "Clicked", but then it doesn't print "about to remove!", why doesn't the 'if deploymech then' work? yogipanda123 120 — 9y
1
edited marcoantoniosantos3 200 — 9y
0
It still doesn't remove the car yogipanda123 120 — 9y

Answer this question