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

I keep getting an error? [Please help]

I'm trying to make a brick, and when you touch the brick it removes your shirt and pants(if your wearing them). and removes your t-shirt. then it clones its shirt and pants inside the model into the player. But I keep getting an error saying: 17:16:18.904 - The Parent property of Shirt is locked, current parent: NULL, new parent Player

Script:

local cln = script.Parent.Pants:clone()

function onTouched(hit)
	local h = hit.Parent:findFirstChild("Humanoid")
	if h~=nil then
		local s = hit.Parent:findFirstChild("Pants")
		if s ~= nil then
			s:Destroy()
			cln.Parent = h.Parent
		elseif s == nil then
			cln.Parent = h.Parent
		end
	end
end

script.Parent.Touched:connect(onTouched)

Same with the shirt script:

local cln = script.Parent.Shirt:clone()

function onTouched(hit)
	local h = hit.Parent:findFirstChild("Humanoid")
	if h~=nil then
		local s = hit.Parent:findFirstChild("Shirt")
		if s ~= nil then
			s:Destroy()
			cln.Parent = h.Parent
		elseif s == nil then
			cln.Parent = h.Parent
		end
		local t = h:findFirstChild("roblox")
		if t ~= nil then
			t:Destroy()
		elseif t == nil then
			print("no tshirt")
		end
	end
end

script.Parent.Touched:connect(onTouched)

2 answers

I didn't quite understand what you meant by then it clones its shirt and pants inside the model into the player But I believe that your error came from line 1: local cln = script.Parent.Pants:clone() and that Isn't quite necessary...so I added cln=s:clone()

function onTouched(hit)
    local h = hit.Parent:findFirstChild("Humanoid")
    if h~=nil then
        local s = hit.Parent:findFirstChild("Pants")
        if s ~= nil then
            s:Destroy()
            cln=s:clone()
            cln.Parent = h.Parent
        elseif s == nil then
            cln.Parent = h.Parent
        end
    end
end

script.Parent.Touched:connect(onTouched)

If there are any errors please reply/comment

> Note: This was not tested out properly, so If there are any mistakes please Comment

-hit.Parent:FindFirstChild will bug if hit.Parent is nil (can occur if you have any projectiles in your place). To fix, conditionally return early: if not hit.Parent then return end

-The error you are getting implies that you've destroyed the object whose parent you're trying to assign

-Your script is currently putting the clone into the humanoid, and then (since onTouched is probably getting triggered multiple times in a row), promptly deleting the clone.

Further, what happens if two people touch the brick? There's only one clone for the both of them.

Corrected script:

local pants = script.Parent.Pants
db = {}
function onTouched(hit)
	if not hit.Parent then return end
	local h = hit.Parent:findFirstChild("Humanoid")
	if not h then return end
	local s = hit.Parent:findFirstChild("Pants")
	if not s then return end
	if db[h] then return end --debounce that allows multiple people to touch the brick simultaneously
	db[h] = true
	pants:Clone().Parent = hit.Parent
	wait() --wait a moment before de-activating the debounce
	db[h] = nil
end
script.Parent.Touched:connect(onTouched)

This script will keep deleting whatever pants they are wearing, even if they are the right ones. If you can change the assetID (or whatever pants use, take a look at their properties) to make the pants change, that would be the preferred method (feel free to test it yourself) -- that way you don't need to clone anything unless they don't have pants in the first place.

1 comment

0

wait it didnt work.

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