Need help with a "grab" script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like a player to be able to, when they click an item, carry it in their mouths (the character is a dog), As well as have a button that appears when they carry the object to drop/destroy it and disappears when the object is dropped or destroyed.
  2. What is the issue? Include screenshots / videos if possible!
    Because the item is cloned when clicked, and then attached to the mouth, more than one are attached. When the button is clicked, one object disappears and then the button disappears.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tweaked the script that clones the object, I couldn’t find anything related on the Developer Hub either.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I will write the scripts’ code down below as the whole concept is hard to explain.

ClickDetector script (located inside the part):

local Item = script.Parent
local CD = Item.ClickDetector
local ItemGrab = game.ReplicatedStorage:WaitForChild("ItemGrab")

CD.MouseClick:Connect(function(player)
	local Char = player.Character
	local JawAttach = Char.Jaw.Attachment
			
		local NewItem = Item:Clone()
		NewItem.Parent = Char
		NewItem.Name = "Item"
	
		local Attach = Instance.new("Attachment")
		Attach.Parent = NewItem

		local Rope = Instance.new("RopeConstraint")
		Rope.Parent = NewItem
		Rope.Attachment0 = Attach
		Rope.Attachment1 = JawAttach
		Rope.Length = 2.5

		ItemGrab:FireClient(player)
	
end)

Button script:

local ItemGrab = game.ReplicatedStorage:WaitForChild("ItemGrab")
local ItemDestroy = game.ReplicatedStorage:WaitForChild("ItemDestroy")

local button = script.Parent
button.Visible = false

ItemGrab.OnClientEvent:Connect(function(player)
	button.Visible = true
end)

button.MouseButton1Click:Connect(function()
	local player = game:GetService("Players").LocalPlayer--I was planning to maybe check if the part is a child of the model

	ItemDestroy:FireServer()
	button.Visible = false
end)

Finally, a Serverscript (located in ServerScriptService)

local ItemDestroy = game.ReplicatedStorage:WaitForChild("ItemDestroy")

ItemDestroy.OnServerEvent:Connect(function(player)
	local Char = player.Character
	local Item = Char.Item
	Item:Destroy()
end)

The problem is that the button is only meant for ONE item, not like 3 or 2 or 4, so the button disappears like it’s supposed to and only one item is destroyed.

If any more/different info is needed please ask!

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

I am not very good at scripting, but that looks fine to me. Maybe someone experienced can help.

3 Likes

if you don’t know, don’t reply :wink:

4 Likes

When you say;

The problem is that the button is only meant for ONE item, not like 3 or 2 or 4, so the button disappears like it’s supposed to and only one item is destroyed.

Do you mean that there are still items remaining in the player after clicking the button?
If this is true, try iterating through the player for instances with the name “Item”.

If I misunderstood, please let me know.

yes, if the player has more than one item cloned, then only one of those items will be destroyed.
if I were to iterate, which script would be best?

1 Like

Try replacing your event response with

ItemDestroy.OnServerEvent:Connect(function(player)
	local Char = player.Character
	for i = 1,#Char:GetChildren() do
		local Pos_Item = Char:GetChildren()[i]
		if Pos_Item.Name == "Item" then
			Pos_Item:Destroy()
		end
	end
end)

This find all instances under Char to delete all with the name of ‘Item’.

This pops up in the output when I click the button ServerScriptService.Destroyscript:7: attempt to index nil with ‘Name’
EDIT: I played around in studio, the amount of items seems to half when the button is clicked, as well as prints the error stated above, not sure how to fix this…

could you maybe explain to me what the code is supposed to do? (not too experienced with for loops, only know how to do for I, v in pairs/ipairs, nothing else really.)

The code was intended to search for everything under the character that was named “Item” and destroy it.

I have no idea what went wrong with it (again not too experienced with for loops)
I did the script so it looks like this:

local ItemDestroy = game.ReplicatedStorage:WaitForChild("ItemDestroy")

ItemDestroy.OnServerEvent:Connect(function(player)
	local Char = player.Character
	for i = 1,#Char:GetChildren() do
		
		local Pos_Item = Char:GetChildren()[i]
		if Pos_Item.Name == "Item" then
			Pos_Item:Destroy()
		end
	end
end)

What pops up in the output is: attempt to index nil with ‘Name’
The error won’t pop up if it’s just one item, only if there is more than one, so the problem isn’t fixed sadly…

Little late on reply, but I put this in the serverscript in serverscriptservice

local ItemDestroy = game.ReplicatedStorage:WaitForChild("ItemDestroy")

ItemDestroy.OnServerEvent:Connect(function(player)
	local char = player.Character
	for i, v in pairs(char:GetChildren()) do
		if v.Name ~= "Item" then
			print("Error, no item found")
			
		else
			v:Destroy()
		end
	end
end)

Aaand it works! The only small thing that bothers me is that, when the item is destroyed, it prints: Error, no item found(36), idk if it printed 36 times, being that the script didn’t tell it to do that. Either way, the initial problem is solved, so thx to all who helped!