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

i get error attempt to call a nil value?

i am trying to make a script that finds number values named "Damage" but when roblox looks in the wrong folder it says attempt to call a nil value and stops the rest of the script

note: I am only going to show a certain part of the script

button.MouseButton1Click:Connect(function(button_1_clicked)
	answer_box.Position = UDim2.new(0, 0, 0, 0)
	answer_box.Transparency = 0
	--find damage value
	local objects = game.Workspace:GetChildren()
	for i, child in ipairs(objects) do
		local childs_children = child:GetChildren()
		local config = childs_children:FindFirstChildOfClass("Configuration")
		local damage_value = config:FindFirstChild("Damage")
		if damage_value then
			local found_value = instance.new("ObjectValue")
			found_value.Name = "damage_found"
			found_value.Parent = button
			found_value.Value = damage_value
		end
	end
end)

1 comment

0

What line is the error on?

1 answer

I think the issue is that damage_value isn't a true or a false and a if statement requires a true or false like this "if true then" or "if false then" or sum like "local nottrue = false if nottrue then" and damage_value isn't any of those so it gives u the nil value. Now some way to fix this is check what the damage value is maybe like

local damage_value = config:FindFirstChild("Damage")

if damage_value == nil then
	print("Damage value was not found")
end

but the main point is that damage_value is not true or false so it wont work with a if statement

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