Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't "If deploymech then" work?

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 comment

1

Why are you using repeat?

2 answers

✓ Accepted answer

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)

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

3 comments

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?

1

edited

0

It still doesn't remove the car

This discussion is preserved as it was. New answers, comments, and votes are closed.