Number value help

So im trying to get a number value of something that is in a tool but it is keep on saying that the tool is not there, so i tried waitforchild and it said that there is a infinate yeild on it…

local axe = plr.Backpack:WaitForChild("Axe")

rp.AddWood.OnServerEvent:Connect(function()
	local dmg = axe.damage
	wood.Value += dmg.Value
end)

When you play the game, is it actually there in your backpack? If so, can you actually see the number value inside?

If the character is holding the tool currently, the tool actually moves out of the backpack hierarchy and goes into the character model instead. If that is the case, you’ll actually want to search the character model for the tool rather than their backpack.

how would i get the axe in the player character, this is what i tried but it says index with nil waitforchild


rp.AddWood.OnServerEvent:Connect(function()
	local axe = player:WaitForChild("Axe")
	local dmg = axe.Damage
	wood.Value += dmg.Value
end)

The following code should find the tool no matter if it’s equipped or not:

local function getCharacterTool(character: Model, name: string): Tool?
	for _, child in pairs(character:GetChildren()) do
		if child.Name == name and child:IsA("Tool") then return child end
	end
	return nil
end

local function getTool(player: Player, name: string): Tool?
	--prioritize searching for the tool inside the player character
	--the tool will be there if it is equipped
	local character = player.Character
	if character then
		local tool = getCharacterTool(character, name)
		if tool then return tool end
	end
	--if we don't find it there, we search in the player backpack
	--it will be there if it's not equipped
	local backpack = player:FindFirstChildWhichIsA("Backpack")
	if not backpack then return nil end
	for _, tool in pairs(backpack:GetChildren()) do
		if tool.Name == name and tool:IsA("Tool") then return tool end
	end
	--we didn't find the tool
	return nil
end

rp.AddWood.OnServerEvent:Connect(function()
	local axe = getTool(plr, "Axe")
	wood.Value += axe.damage.Value
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.