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

How do I detect a tool in a character?

So simply put I want this script to detect which weight is in the back pack. If its W0 I want to give 10 points and W1 gives 20 points and so on. The first weight works and adds the value of 10 to the strength leaderboard. But for some reason this script cant find W1 in the character. The weight that I can detect is W0 which is placed in the starterpack for first time players.

Notes:

  • W0 through W4 is the name of each of the weights names. These are the tool holder. These 4 tools are in lighting
  • Cloning process to character works.

Question: Why cant this script find tools W1 through W4 in the character even though they are successfully cloned and I can see that they are in the character.

The purpose of this is to figure out which weight it is to add the different strength values to leader board. So if I have weight W1 I want it to add 20 but I cant add it if I dont know which one the player has.

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local starterRebirthAmount = 100
local coolDown = 1

replicatedStorage.Remotes.Lift.OnServerEvent:Connect(function(player)
	if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
	
	local debounce = remoteData[player.Name].Debounce
	
	if not debounce.Value then
		debounce.Value = true
			local Animation = Instance.new("Animation")
		--Animation.AnimationId = "http://www.roblox.com/asset/?id=507771019"
		Animation.AnimationId = "rbxassetid://4479273224"
		local LoadedAnimation = game.Workspace[player.Name].Humanoid:LoadAnimation(Animation)
		LoadedAnimation:Play()
		local toolsInPackpack = game.Workspace[player.Name]:GetChildren()
		local enTool  = game.Workspace[player.Name]:FindFirstChildOfClass("Tool") 
		print(enTool)
		print("a")
		for i,v in pairs(toolsInPackpack) do
			print(v.Name..v.ClassName)
			if v.ClassName == "Tool" then
				print("c")
				if game.Workspace[player.Name] then
					print("d")
					local char = game.Workspace[player.Name] 
					if v:findFirstChild("Weight") then
						print("e")
						local weight = v:findFirstChild("Weight")
						if weight.Value == 0 then
							player.leaderstats.Strength.Value = player.leaderstats.Strength.Value+10*(player.leaderstats.Rebirths.Value + 1)
						elseif weight.Value == 1 then
							player.leaderstats.Strength.Value = player.leaderstats.Strength.Value+20*(player.leaderstats.Rebirths.Value + 1)
						elseif weight.Value == 2 then
							player.leaderstats.Strength.Value = player.leaderstats.Strength.Value+40*(player.leaderstats.Rebirths.Value + 1)
						elseif weight.Value == 3 then
							player.leaderstats.Strength.Value = player.leaderstats.Strength.Value+90*(player.leaderstats.Rebirths.Value + 1)
						elseif weight.Value == 4 then
							player.leaderstats.Strength.Value = player.leaderstats.Strength.Value+150*(player.leaderstats.Rebirths.Value + 1)
						end
					end
				end
			end
		end
			
		wait(coolDown)
		
		debounce.Value = false
	end
	
end)

3 answers

local Players = game:GetService("Players"); -- define players.

--//Backpack\\--

for i, v in pairs(Players.LocalPlayer.Backpack:GetChildren()) dp
	if v:IsA("Tool") then
		-- Tool found
	end;
end;

--//If Tool Is Equipped\\--

for i, v in pairs(Players.LocalPlayer.Character:GetChildren()) do
	if v:IsA("Tool") then
		-- Tool found
	end;
end;
local Players = game:GetService:Players

for i,v in pairs(Players:GetPlayers()) do -- Check if tool is equipped for any player
	for i,a in pairs(v.Character:GetChildren()) do
		if a:IsA('Tool') then
			print('A player with the name of  '..v.Name..' is holding a tool with the name of '..a.Name)
end
end
end

for i,v in pairs(Players:GetPlayers()) do -- Check if tool is in a backpack for any player
	for i,a in pairs(v:FindFirstChild('Backpack'):GetChildren()) do
		if a:IsA('Tool') then
			print('A player with the name of  '..v.Name..' has a tool with the name of '..a.Name..' in his backpack')
end
end
end

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