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

Conditional statements throwing exceptions?

For some reason certain conditional statements are tossing errors when performing otherwise standard operations under normal circumstances.

Basically

My if statements are searching for a handle in parts/tools that do not have a handle, and are attempting to compare positional values with tools to parts, where as tools do not hold a position. Ultimately erroring out and effectively breaking absolutely everything. I believe a Roblox update is to blame but if anyone knows a way to fix this, or if it's the fact I'm attempting to handle this on the client side. Please inform me.

RE.OnClientEvent:Connect(function(spawned)
	for i,v in pairs(spawned) do
		local x = PickupPrompt:Clone()
		x.TextLabel.Text = "[F] Pick Up: "..v.Name
		x.Parent = v
		x.Enabled = true
		game:GetService("UserInputService").InputBegan:Connect(function(input,gpe)
			if gpe then return end
			if input.KeyCode == Enum.KeyCode.F then 
				if (player.Character.Torso.Position - v:FindFirstChild("Handle").Position).Magnitude < 10 then -- RUNS BOTH THIS
					print("prolly gun")
				elseif (player.Character.Torso.Position - v.Position).Magnitude < 10 then --AND THIS FOR ALL INDEXED INSTANCES
					print("prolly mag")
				end
			end
		end)
	end
end)

1 answer

A roblox update as far as I can tell is probably not to blame.

You're currently doing two things inside your if statement, the findFirstChild for the handle and the subtraction.

You should instead consider wrapping the subtraction inside of another if statement to check if the handle exists.

Example:

local Handle = v:FindFirstChild("Handle")

if v~=nil then
	if (player.Character.Torso.Position-Handle.Position).magnitude < 10 then
	end
end

Anyways hope that helps.

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