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
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!
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