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

Remove hats OnTouch?

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

3 comments

0

Please only post questions...

0

Sorry, realized I didn't point out what was wrong.

0

Change 'classname' to 'ClassName'. Lua is case sensitive (at least most of the time). Also remove() is deprecated, use Destroy() instead.

1 answer

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.

1 comment

0

Did you forget this isn't Javascript? From my knowledge, the semi colons would return syntanx errors.

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