I was writing a script that removes a part named "Gold" or "Xp" when it touches it. This is the code :
script.Parent.Touched:connect(function(hit) print(hit) if hit.Name == "XP" or "Gold" then print("ye") hit:remove() else print("nope") end end)
It doesn't work, and it removes the player instead. Can you help me with this?
Any help is appreciated. Thanks!
You made a small Mistake on line 03
, Let me show you:
script.Parent.Touched:connect(function(hit) if hit.Name=="XP" or hit.Name=="Gold" then-- on the gold part. hit:remove() end end)
The only problem I noticed at first glance was the checking of the Name
property's value.
Also, since you want the hit to be from a part
, i added a check for hit's ClassName
using the
IsA
method.
If the first argument is false or nil (the name isn't 'XP' in this context), the or
operator returns the second argument, which is a string
(true, because it isn't nil or false).
if (false or true) then --will run because true if (nil or true) then --will run because true
If the first argument is true, it is returned and the second argument is ignored.
if (true or true) then --will also run because true
The statement then only checks for whether or not the Name is XP
. Here's are examples on how to work around this
or
script.Parent.Touched:connect(function (hit) if hit:IsA('BasePart') then if hit.Name == 'XP' or hit.Name == 'Gold' then warn('Destroy') else -- not a match end end end)
Names = {'XP', 'Gold'} script.Parent.Touched:connect(function (hit) if hit:IsA('BasePart') then if Names[hit.Name] then warn('Destroy') else -- not a match end end end)