What's wrong with my script? [Solved]

  1. What do I want to achieve? I want to make a button that removes a model inside the player.

  2. What is the issue? When I scripted it, the script does nothing.

  3. What solutions have you tried so far? I have tried to find anything on the Devforum and YouTube. Sadly nothing.

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

script.Parent.Touched:connect(onTouched) 
1 Like
function onTouched(hit) 
	local d = hit.Parent:GetChildren() 
	for i, v in pairs(d) do
        if v:FindFirstChild('Bandages') then
           v:FindFirstChild('Bandages'):Destroy()
        end
    end
end 

script.Parent.Touched:connect(onTouched)
1 Like

“:remove()” Does not exist. You would have to replace this with “:Destroy()”, as shown in the above reply.

1 Like

Thanks for the help! But as it seems it does nothing too.

try this

function onTouched(hit) 
	local d = hit.Parent:GetChildren() 
	for i, v in pairs(d) do
        if v.ClassName == "Bandages"
              v:Destroy()
        end
    end
end 

script.Parent.Touched:connect(onTouched)

Thanks for the help, but as it seems this doesn’t work too. Roblox also took this as an “error”
roblox takes it as a mistake

forgot to add then to the if loop

if v.ClassName == "Bandages" then
              v:Destroy()
        end
1 Like

Thanks for the help, but this sadly didn’t do anything. I’m worried that I won’t be able to fix this.

Where is the script located? In the button part I assume?

Yes. It is located in the button part.

Since I don’t really know how your models are structured here’s my idea:

function onTouched(hit) 
	-- get all the parts in the player model
	local d = hit.Parent:GetDescendants() 
	-- loop all the parts in the model (variable d)
	for i, v in pairs(d) do
		-- check if the name is Bandages
		if v.Name == "Bandages" then
			-- if so delete it
			v:Destroy()
		end
	end
end 

script.Parent.Touched:connect(onTouched)

Depending on what are bandages (and where they are located) this should work for most cases

1 Like

This is the solution! Thank you so much!

Oops. On mobile so i forgot to add it. My bad.

2 Likes