Working on a vending machine and trashcan script, but get an error

Hey all! I’m a bit new to scripting, but I’m currently working on a vending machine and trashcan script! I made it so that if you have an item in your inventory, you are not allowed to use the machine. The trashcan follows the same premise, that you are not allowed to throw the item away if you have nothing in your inventory.

However, while the scripts seem to work perfectly fine, I get the error of “The Parent property of Starjuice is locked, current parent: NULL, new parent Backpack” on line 17 of my Vending machine script. I’ve read that its because the item gets deleted, but I am unsure how to fix.

So far, I have tried to double clone the object to prevent it from being “deleted”, as well as had my script clear the players backpack instead of delete the item. Clearing the backpack is a better change for my script and is the one in use as of right now, but the error still occurs.

Any advice will definitely help! Thank you! Here are the scripts!

This is the vending machine script!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local drinkTool = ReplicatedStorage.Starjuice
local ToolName = "Starjuice"
local drinkCopy = drinkTool:Clone() --Clones drink
local drinkPart = script.Parent

drinkPart.Attachment.ProximityPrompt.Triggered:Connect(function(player) --ProximityPrompt cleared
	if player.Backpack:FindFirstChild(ToolName) then
		print("Player has tool in backpack!")

	else
		print("Player does not have tool in backpack!")
		script.Parent.Attachment.Thud:Play()
		wait(.5)
		script.Parent.Attachment.Clunk:Play()
		drinkCopy.Parent = player.Backpack --Puts drink in inventory
	end
end)

And here is the trashcan script!

local Click = game.Workspace.Trashcan.trashtop.ClickDetector
local Message = game.ReplicatedStorage.Trash
local ToolName = "Starjuice"

Click.MouseClick:connect(function(Player)--Click prompt triggered
		if Player.Backpack:FindFirstChild(ToolName) then
		print("Player has tool in backpack!")
		game.Workspace.Trashcan.trashtop.Ding:Play()
		Player.Backpack:ClearAllChildren() --What is supposed to delete inventory!
		
		else
			print("Player does not have tool in backpack!")
		end
	end)

Thank you for reading!

1 Like

Try using this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local drinkTool = ReplicatedStorage.Starjuice
local ToolName = "Starjuice"
local drinkPart = script.Parent

drinkPart.Attachment.ProximityPrompt.Triggered:Connect(function(player) --ProximityPrompt cleared
	if player.Backpack:FindFirstChild(ToolName) then
		print("Player has tool in backpack!")

	else
                local drinkCopy = drinkTool:Clone() --Clones drink
		print("Player does not have tool in backpack!")
		script.Parent.Attachment.Thud:Play()
		wait(.5)
		script.Parent.Attachment.Clunk:Play()
		drinkCopy.Parent = player.Backpack --Puts drink in inventory
	end
end)
1 Like

Basically move the drinkClone variable into when the prompt is triggered.

Took a bit because studio was acting funky, but this works perfectly! Thank you! A minor mistake on my part, I’ll be more wary of where the variables go next time lol! Thank you so much again!

No problem! Good luck with the game!

1 Like