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

My touch script deletes everything not named guard! How do I fix?

Asked by 3 years ago

I want it to make it delete parts that are named guard but instead it deletes other parts! How do i fix this?

function ds(hit)
    if hit.Name ~= "Guard" then

    hit:Destroy()


    end


end
script.Parent.Touched:Connect(ds)

5 answers

Log in to vote
1
Answered by 3 years ago

Either like this..

function ds(hit)
    if hit.Name ~= "Guard" then
    --hit:Destroy()

    else
    hit:Destroy()
    end


end
script.Parent.Touched:Connect(ds)

or like this

function ds(hit)
    if hit.Name == "Guard" then
    hit:Destroy()
    end


end
script.Parent.Touched:Connect(ds)
Ad
Log in to vote
0
Answered by 3 years ago

Simple fix. Your script makes it so that parts that are not equal, or ~=, to “Guard” will be deleted. To fix this, just remove the tilde.


function ds(hit) if hit.Name = "Guard" then hit:Destroy() end end script.Parent.Touched:Connect(ds)
0
It has a error on the equal but how snowpototoy 37 — 3y
0
~= is for checking if [blank] is not equal to [blank]. == is for checking if [blank] is equal to [blank]. = is for telling the script that [blank] is equal to [blank]. >= is used when checking if a number is more than a different number. And <= is for checking if a number is less than a different number. The reason you get an error on the equals sign is because it should be two equals. Desmondo1 121 — 3y
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

On line 2, you put "If hit.name ~= Guard then", what's wrong about that is this: "~=". Basically "~=" means not equal, since you put that there it will destroy everything that is not equal to "Guard". So basically it destroys everything that is not named "Guard". You can easily fix it by switching "~=" to "==". Good luck making your game :)

function ds(hit)
    if hit.Name == "Guard" then
        hit:Destroy()
    end
end

script.Parent.Touched:Connect(ds)
Log in to vote
0
Answered by
notfenv 171
3 years ago
local Brick = script.Parent

function OnTouched(Hit)
    if Hit.Name == "Guard" then
        Hit:Destroy()
    end
end

Brick.Touched:Connect(OnTouched)

Simple.

Log in to vote
0
Answered by 3 years ago

First of all you said if it is not named guard then destroy everything that is not named guard. It should look more like this.

01  function ds(hit)
02      if hit.Name == "Guard" then
03   
04      hit:Destroy()
05   
06   
07      end
08   
09   
10  end
11  script.Parent.Touched:Connect(ds)


Answer this question