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

Tool check not working? Not updating if the player has tool?

I have a gun shop in my game. And whenever you buy a gun, where it says buy turns into equip. All of that works perfectly fine. Except whenever you try to equip it. I dont know how to update it so it checks if the player has the tool. This is the script:

player = game.Players.LocalPlayer
gun = player.StarterGear
Gunslot = player.GunSave
if script.Disabled == false then
	script.Parent.MouseButton1Click:Connect(function()
		if gun:FindFirstChild("Webley Mk IV") then
			script.Value.Value = true
		else
			script.Value.Value = false
			script.Value.Changed:Connect(function()
				if script.Value.Value == false then
					local a = gun["Webley Mk IV"]:Clone()
					local b = a:Clone()
					a.Parent = Gunslot
					b.Parent = player.Backpack
				end
			end)
	 	end
	end)
end

1 answer

✓ Accepted answer

Well, there are two ways to solve your problem. I assume you are asking how to check when player have a tool in backpack. First, this method uses ChildAdded Event.

https://developer.roblox.com/en-us/api-reference/event/Instance/ChildAdded

Example code :

local backpack = game.Players.LocalPlayer:WaitForChild("Backpack") -- Gets the player's backpack

backpack.ChildAdded:Connect(function(object) -- Fires when a child is added, object is the instance added
	if object:IsA("Tool") and object.Name == "Tool" then -- Checks whether the added item is tool, and the name of the object, here, is 'Tool'. Change that to your whatever tool name.
		print(object.Name) -- Prints the name of the tool
	end
end)

Second, we will be using ChildAdded as well as a for loop.

Example code :

local backpack = game.Players.LocalPlayer:WaitForChild("Backpack") -- Gets the player's backpack

backpack.ChildAdded:Connect(function(object) -- Fires when a child is added, object is the instance added
	-- Last time, we check the object added. But now, we will be looping through the backpack.
	local backpackContents = backpack:GetChildren() -- Creates an array of all the stuff in the backpack
	
	for _, tool in ipairs(backpackContents) do
		if tool:IsA("Tool") -- Checks that the object is tool, here, the object is one of the child of backpack selected by the loop
			if tool.Name == object.Name then -- Verifies the name
				print("Found")
			end
		end
	end
end)

The second method is useful when you want to do something with the other things in the backpack, rather than the object added.

Lemme know if it helps!

1 comment

0

Thanks so much! Have a good day!

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