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

What causes a script to not work after it is cloned?

Original works until it is the model is cloned.


--give cookie tray (put in inven, remove cookie tray, turn off oven
	script.Parent.ClickDetector.MouseClick:connect(function(player)
		local foodinventory = player:FindFirstChild("BakedGoods")
		if foodinventory ~= nil then
			print("yes")
		local alreadyexists = foodinventory:FindFirstChild("ChocChipCookies")
		if alreadyexists == nil then
			print("yeet")
			local tag = Instance.new("NumberValue",foodinventory)
			tag.Name = "ChocChipCookies"
			tag.Value = tag.Value + 12		
		else
			print("yoot")
			alreadyexists.Value = alreadyexists.Value + 12  --instead of making a new value, adds 12 to existing inventory value
			end	
		end
		
		workspace.Oven1.On.Value = false --turn off oven (will make oven w/player id tag attached later)
		script.Parent.Parent:remove()
		
	end)

6 comments

0

It doesn't work for me either, but what if you have the model cloned and pasted BEFORE the script breaks? Still doesn't work

1

Use :Connect() and :Destroy()

1

:remove() is domesticated :destroy(). also, :Connect() is :connect() but capitalized.

2

remove() isn't a pet @Fifkee, so it can't be domesticated

0

LOL

0

laughs

1 answer

Try this, if you disable the script before it is cloned, then enable it once it's in the Workspace, then it will work normally. Here is my example: Script within a Part, within a Model, within Workspace

-- give cookie tray, put in Oven, remove cookie tray, turn off oven
script.Parent.ClickDetector.MouseClick:Connect(function(player)
	local foodinventory = player:FindFirstChild("BakedGoods")
	if foodinventory ~= nil then
		print("yes")
		local alreadyexists = foodinventory:FindFirstChild("ChocChipCookies")
		if alreadyexists == nil then
			print("yeet")
			local tag = Instance.new("NumberValue", foodinventory)
			tag.Name = "ChocChipCookies"
			tag.Value = tag.Value + 12     
		else
			print("yoot")
			alreadyexists.Value = alreadyexists.Value + 12  --instead of making a new value, adds 12 to existing inventory value
		end
	end
	
	workspace.Oven1.On.Value = false --turn off oven (will make oven w/player id tag attached later)
	script.Parent.Parent:Destroy()
end)

I changed a few of the connections, for instance, remove is deprecated, and connect is now Connect. Script within ServerScriptService

while true do
	local model = workspace:FindFirstChild("Cook")
	if model == nil then
		local model2 = game.ServerStorage.Cook:Clone()
		model2.Parent = workspace
		model2.Part.Script.Disabled = false -- this is the location of the script with the above function
	end
	wait()
end

This doesn't have to be a continuous loop, but I used one to test that it works. You need to make sure that the script is disabled normally when in studio (since the function will enable it)

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