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

How do i make a hat remover? I do not know how to make one.

Asked by 6 years ago
Edited 6 years ago

function onTouched(hit) local d = hit.Parent:GetChildren() for i=1, #d do if (d[i].className == "Accessory") then d[i]:remove() end end end : This is the script I tried.

3 answers

Log in to vote
0
Answered by 6 years ago

You didn't provide the function a reason to fire, so it's not doing anything

function onTouched(hit)
    local d = hit.Parent:GetChildren()
    for i =  1, #d do
        if d[i]:IsA("Accessory") then
            d[i]:Destroy()
        end
    end
end

script.Parent.Touched:Connect(onTouched)

Line 10, script.Parent.Touched:Connect(onTouched), tells the function to fire when the part the script is inside gets touched.

Also on line 5, d[i]:Destroy(), you had d[i]:Remove() instead, however the "Remove" function is deprecated (https://wiki.roblox.com/index.php?title=API:Class/Instance/Remove), which means it shouldn't be used anymore (https://wiki.roblox.com/index.php?title=Deprecation)

Ad
Log in to vote
0
Answered by
GingeyLol 338 Moderation Voter
6 years ago
Edited 6 years ago

you could use the for i,v in pairs function

parenthesis around conditional statements are redundant

instead of className the property is ClassName and the basic parameter :IsA would be more efficient

:remove() is depecrated and :Destroy() should be used

[OPTIONAL] you could also check if what touches the object is a player with an additional parameter

obviously, it wouldn't have worked since it isn't fired

normal script

local function accessories(object)
    wait() --time
    for i,things in pairs(object:GetDescendants()) do 
        if things:IsA("Accessory") then
            things:Destroy()
        end
    end
end

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
         accessories(character)
    end)
end)

also use

Code block

next time

additional wiki links YOU SHOULD READ https://wiki.roblox.com/index.php?title=API:Class/Instance/IsA http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#pairs

0
Well, this removes all hats upon a player joins. vicente010101 2 — 6y
Log in to vote
0
Answered by 6 years ago

Please provide more information next time you post a question and include code block. Take a look here to learn more about how to format a question. Also you should take a look at how to post good questions and answers. You can also preview your question to see how it will look before you post it.

Hats have been changed to Accessory which connect using the Attachments so both should be checked at the current time.

The only way we are able to tell what a accessory connects to is to check the attachment name as this attachment name connects to the same attachment name within the characters model.

A quick example:-

local p = Instance.new('Part')
p.Anchored = true
p.CFrame = CFrame.new(5, 1, 5)
p.Parent = workspace

p.Touched:Connect(function(hit)
    if not game:GetService('Players'):GetPlayerFromCharacter(hit.Parent) then return end

    for _, itm in pairs(hit.Parent:GetChildren()) do
        -- check for accessory with a attachment called HatAttachment
        if itm:IsA('Accessory') 
            and itm:FindFirstChild('Handle') 
            and itm.Handle:FindFirstChild('HatAttachment')

            -- check for hat
            or  itm:IsA('Hat')  then

            itm:Destroy() -- destroy
        end
    end
end)

Lastly you should also not use deprecated things within Roblox as they will be removed in the future.

I hope this helps. Please comment if you do not understand how / why this code works.

Answer this question