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

Remove hats OnTouch?

Asked by
Mystdar 352 Moderation Voter
10 years ago

This script doesn't work and no Output helps here, please point out what is incorrect, thank you.

function onTouched(hit)
    local C = hit.Parent:GetChildren()
        if C.classname == "Hat" then
            C:remove()
        end
end

script.Parent.Touched:connect(onTouched)

Thanks

0
Please only post questions... Muoshuu 580 — 10y
0
Sorry, realized I didn't point out what was wrong. Mystdar 352 — 10y
0
Change 'classname' to 'ClassName'. Lua is case sensitive (at least most of the time). Also remove() is deprecated, use Destroy() instead. Perci1 4988 — 10y

1 answer

Log in to vote
1
Answered by 10 years ago

As stated by Perci1 in the comments, responding to your post, Lua is case-sensitive and standardly utilizes Camel Case. Therefore, the property ClassName must be written as designated earlier in this post. Additionally, you do not access all children simply through GetChildren(). It returns a table.

A table can be iterated through using the next function in a for-loop.

Touched does not only return Character interaction, therefore it would be wise to perform a conditional check of the object before proceeding.

The fixed code would look as follows:

function onTouched(hit)
if not(hit.Parent:FindFirstChild'Humanoid') then return end;
for i,v in next,hit.Parent:children'' do
if(v.ClassName=='Hat') then
v:Destroy();
end;
end;
end;

script.Parent.Touched:connect(onTouched)

Hope this helped. Also, remember that the wiki is a helpful place to go for scripting functionality in RBX.lua.

0
Did you forget this isn't Javascript? From my knowledge, the semi colons would return syntanx errors. Octillerysnacker 115 — 10y
Ad

Answer this question